熊没有正确创建

问题描述:

我正在建立我的第一个MEAN栈应用程序使用this苏格兰文章。熊没有正确创建

我已经到了要发送POST请求到我的localhost通过邮递员创建新的熊(模型)的点。

的问题是,当我将请求发送到看到所有的熊我回来了以下内容:

[ 
    {"_id":"589f6ecc734d1d56393c9444","name":"Klaux"}, 
    {"_id":"589f6f924af04a03d8870dea","__v":0} 
] 

数组中的第一个对象是我在MongoDB中手动创建时,我创建了一个例子新document。 (阵列中的第二个目的)如上所述,当我经由邮差不正确创建它发送一个请求用于创建

这里的发送请求的代码:

// /api/bears 
router.route('/bears') 
    .get(function(req, res) { 
    Bear.find(function(err, bears) { 
     if (err) 
     res.send(err); 
     console.log('Bears',bears); 
      res.json(bears); 
    }); 
    }) 
    .post(function(req, res) { 
    var bear = new Bear();  // create a new instance of the Bear model 
    bear.name = req.body.name; // set the bears name (comes from the request) 

    // save the bear and check for errors 
    bear.save(function(err) { 
     if (err) 
     res.send(err); 
      res.json({ message: 'Bear created!' }); 
    }); 
}); 

app.use('/api', router); 

和数据库收集看起来是这样的:

MongoDB mLab

那么,为什么没有被正确创建的记录?

编辑

熊模型是这样的:

var mongoose = require('mongoose'); 

var BearSchema = new mongoose.Schema({ 
    name: String 
}); 
module.exports = mongoose.model('Bear', BearSchema); 
+0

你的熊模型是什么样的? –

+0

@LuisDiegoHernández看我的编辑 – Katana24

这实际上是要求在评论部分在文章底部的回答。 这里是一个总结:

当发送从Postmanx-www-form-urlencoded选项不应该设置。 相反,它应该设置为raw并作为JSON传递。看下面的截图:

enter image description here