簡體   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');
    });
});

后來,當我嘗試使用 db.contacts.find() 放置數據並嘗試查看來自 mongo shell 的數據時,數據庫中沒有數據。 只有一個 object ID,沒有別的。 這就是我在 Mongo Shell 中看到的——

Mongo shell圖片

只需在 app.js 中嘗試

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

並檢查請求的 header 因為應該有

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM