简体   繁体   English

无法使用 Mongoose 在 MongoDB 中保存和显示数据

[英]Unable to save and show data in MongoDB using Mongoose

app.post('/contact', (req, res)=> {
    var myData = new Contact(req.body);
    myData.save().then(()=>{
        res.send("Item has been saved");
    }).catch(()=>{
             res.send('Item was not saved due to some error');
    });
});

Later when I'm trying to put data and trying to see data from mongo shell using db.contacts.find(), the data is not there in the database.后来,当我尝试使用 db.contacts.find() 放置数据并尝试查看来自 mongo shell 的数据时,数据库中没有数据。 There is just an object ID and nothing else.只有一个 object ID,没有别的。 This is what I'm seeing in the Mongo Shell--这就是我在 Mongo Shell 中看到的——

Picture of Mongo shell Mongo shell图片

just try in app.js只需在 app.js 中尝试

const bodyParser = require('body-parser');
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: false}))

and check the header of request because should have并检查请求的 header 因为应该有

  headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json'
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM