Mongo .get 返回垃圾
Mongo .get returning junk
当我从类似 Postman:
的东西执行这个函数时
router.get('/db', function(req, res, next) {
tune.find({}, function (err, results) {
res.json(results);
});
});
我的数据库returns这个:
[{"_id":"56f30425ba97bb301fe6ab1a","__v":0},
{"_id":"56f30514f9b7ea3b1f1fd9f7","__v":0},
{"_id":"56f306bb9c8203451f2cc58a","__v":0},
{"_id":"56f306ca9c8203451f2cc58b","__v":0},
{"_id":"56f306e99c8203451f2cc58c","__v":0},
{"_id":"56f33d43b64d540b208b6c3c","__v":0}]
我的猫鼬模式:
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);
});
当我从 Postman 执行 post 命令时,我收到一条成功的 post 消息。这就是我返回 JSON 的方式吗?我希望能够看到数据库中每个 post 的标题、艺术家和流派。
谢谢
在这种情况下,Mongoose 根本没有保存您期望的内容。尝试在调试器中查看 req.body
和 tune1
以确保您获得预期的结果。
在您的架构中将 strict
设置为 'throw'
也可能会有所帮助,这样当我们尝试保存无效的 tune
:
var track = new Schema({
title: String,
artist: String,
genre: String
}, {
strict: 'throw'
});
当我从类似 Postman:
的东西执行这个函数时router.get('/db', function(req, res, next) {
tune.find({}, function (err, results) {
res.json(results);
});
});
我的数据库returns这个:
[{"_id":"56f30425ba97bb301fe6ab1a","__v":0},
{"_id":"56f30514f9b7ea3b1f1fd9f7","__v":0},
{"_id":"56f306bb9c8203451f2cc58a","__v":0},
{"_id":"56f306ca9c8203451f2cc58b","__v":0},
{"_id":"56f306e99c8203451f2cc58c","__v":0},
{"_id":"56f33d43b64d540b208b6c3c","__v":0}]
我的猫鼬模式:
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);
});
当我从 Postman 执行 post 命令时,我收到一条成功的 post 消息。这就是我返回 JSON 的方式吗?我希望能够看到数据库中每个 post 的标题、艺术家和流派。
谢谢
在这种情况下,Mongoose 根本没有保存您期望的内容。尝试在调试器中查看 req.body
和 tune1
以确保您获得预期的结果。
在您的架构中将 strict
设置为 'throw'
也可能会有所帮助,这样当我们尝试保存无效的 tune
:
var track = new Schema({
title: String,
artist: String,
genre: String
}, {
strict: 'throw'
});