Mongo。回复垃圾

当我从Postman执行这个函数时:

router.get('/db', function(req, res, next) { tune.find({}, function (err, results) { res.json(results); }); }); 

我的数据库返回这个:

 [{"_id":"56f30425ba97bb301fe6ab1a","__v":0}, {"_id":"56f30514f9b7ea3b1f1fd9f7","__v":0}, {"_id":"56f306bb9c8203451f2cc58a","__v":0}, {"_id":"56f306ca9c8203451f2cc58b","__v":0}, {"_id":"56f306e99c8203451f2cc58c","__v":0}, {"_id":"56f33d43b64d540b208b6c3c","__v":0}] 

我的mongoose模式:

 var Schema = mongoose.Schema; var track = new Schema({ title: String, artist: String, genre: String }); var tune = mongoose.model('tune', track); 

我的post:

 router.post('/db', function(req, res, next) { var tune1 = new tune(req.body); tune1.save(function (err) { if (err) { console.log('error!');} else { res.json({message: 'Track successfully posted'}); } }); }); 

请求post:

 app.use('/users', userRoutes); var options = { method: 'POST', url: 'http://localhost:3000/users/db', headers: { 'content-type': 'application/x-www-form-urlencoded', 'postman-token': '', 'cache-control': 'no-cache' }, form: { title: '000000', artist: 'blah blah', genre: 'rap' } }; request(options, function (error, response, body) { if (error) throw new Error(error); console.log(body); }); 

当我从邮递员那里发布邮件命令时,我会收到一封成功的邮件。 这只是我返回JSON的方式吗? 我希望能够看到DB中每个post的标题,艺术家和stream派。

谢谢

在这种情况下,mongoose根本就没有保存你所期望的。 尝试在debugging器中查看req.bodytune1 ,以确保获得预期的结果。

它也可能有助于strict设置'throw'模式”,只是当我们试图保存一个无效的tune时,我们会得到一个错误:

 var track = new Schema({ title: String, artist: String, genre: String }, { strict: 'throw' });