我正在使用this Scotch文章构建我的第一个MEAN堆栈应用程序。
我已经达到了向POST
发送localhost
请求以通过Postman创建新熊(模型)的程度。
问题在于,当我发送查看所有熊的请求时,我会回复以下内容:
[
{"_id":"589f6ecc734d1d56393c9444","name":"Klaux"},
{"_id":"589f6f924af04a03d8870dea","__v":0}
]
数组中的第一个对象是我在创建新document
时手动在MongoDB中创建的示例。如上所述,当我通过Postman发送创建请求时,它没有正确创建(数组中的第二个对象。)
这是发送请求的代码:
// /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);
数据库集合如下所示:
那么为什么没有正确创建记录?
修改
熊模型看起来像这样:
var mongoose = require('mongoose');
var BearSchema = new mongoose.Schema({
name: String
});
module.exports = mongoose.model('Bear', BearSchema);