首页 文章

Mongoose不会在本地将文档保存到我的数据库

提问于
浏览
1

我想我的问题很简单 . 我可以运行mongoose将文档保存到我在mlab上的数据库中 . 也可以通过邮递员在获取请求中查看保存的数据 . 但是当我使用本地mongodb而不是mlab然后尝试通过运行“mongo”在我的终端上显示数据库集合然后选择与mongoose连接的相同数据库它显示我一个空数据库 . 我再次尝试发送带有新数据的发布请求..并且结果是相同的,对邮递员和mlab是正面的,但是当连接到本地mongodb并发布新数据时......根本就没有任何内容 . 我可以通过命令insert()将数据插入shell的唯一方法 .

知道为什么会这样吗?先感谢您 .

app.js

var express = require("express");
        var app = express();
        var port = 8080;
        var bodyParser = require('body-parser');
        app.use(bodyParser.json());
        app.use(bodyParser.urlencoded({ extended: true }));

        var mongoose = require("mongoose");
        mongoose.Promise = global.Promise;
        mongoose.connect("mongodb://localhost:27017/test",{ useNewUrlParser: true });


        // Model & Schema
        var nameSchema = new mongoose.Schema({
            firstName: String,
            lastName: String
        });
        var User = mongoose.model("User", nameSchema);



        // Routes
        app.get('/users', (req, res) => {
            User.find({}, (err, user) => {
                if (err) throw err;
                console.log(user)
                res.send(user)
            })
        })
        app.post("/addname", (req, res) => {
            var myData = new User(req.body);
            myData.save()
            .then(item => {
            res.send("item saved to database");
            })
            .catch(err => {
            res.status(400).send("unable to save to database");
            });
        });



        app.use("/", (req, res) => {
            res.sendFile(__dirname + "/index.html");
        });

        app.listen(port, () => {
        console.log("Server listening on port " + port);
        });

在这里我使用mlab:

enter image description here

enter image description here

enter image description here

但使用本地机器..:

enter image description here

enter image description here

enter image description here

1 回答

  • 1

    尝试下面添加api

    app.post("/addname", (req, res) => {
        req.body = JSON.parse(JSON.stringify(req.body)); //This line is Optional, some time req.body is in json buffer form so we are converting to pure json
        User(req.body).save().then(item => {
            console.log("DB Item saved --->", item);
            res.status(200).send("item saved to database");
        }).catch(err => {
            res.status(400).send("unable to save to database");
        });
    });
    

    别忘了req.body必须包含firstName,lastName

    关于收集(架构)文件列表的问题更新后

    你正在努力

    db.test.find()

    因为你的模式名称是User并且将保存为用户,因为mongodb更喜欢Plural集合名称,因为它包含多个文档

    你必须尝试

    在你的mongo shell中 db.users.find()

    它将返回用户集合的所有文档 .

    如果需要特定的集合名称(模式名称),则必须创建如下的模式

    // Model & Schema
    var nameSchema = new mongoose.Schema({
        firstName: String,
        lastName: String
    }, { collection: 'User' });
    var User = mongoose.model("User", nameSchema);
    

    在mongo shell你必须尝试

    db.User.find();
    

    要列出mongo shell中的所有集合,您必须尝试如下

    db.getCollectionNames()
    

相关问题