猫鼬响应奇怪的格式
mongoose response odd format
我想做的是让用户获得他们已添加到收藏夹的 post 的列表,只需将他们的 _id 放入 post 即可收藏夹数组。
当 运行 该特定查询时,它作为一个数组返回,其中包含未命名的条目以及数组中的 _locals。
我需要更像 Posts: [ {}, {} ], _locals.
如何将其转换成那种格式?
var UserSchema = new Schema({
username : String,
firstName : String,
lastName : String,
email : String,
password : String
});
var PostSchema = new Schema({
author : { type: Schema.Types.ObjectId, ref: 'User' },
date : Date,
body : String,
username : String,
sid : String,
views : Number,
likes : [{ type: Schema.Types.ObjectId, ref: 'User' }],
favorites : [{ type: Schema.Types.ObjectId, ref: 'User' }]
});
and the get
app.get('/favorites', function(req, res) {
Post
.find({ 'favorites': '58f4f9e5650785228016287f'})
.exec(function(err, favorites) {
res.render('favorites', favorites)
console.log(favorites)
})
});
I get a response of
[ { _id: 58f4f9e96507852280162880,
author: 58f4f9e5650785228016287f,
body: 'test',
date: 2017-04-17T17:22:49.059Z,
username: 'test',
sid: 'BJ-xquGRl',
__v: 2,
favorites: [ 58f4f9e5650785228016287f ],
likes: [] },
{ _id: 58f500edd8abc7242c90d61c,
author: 58f4f9e5650785228016287f,
body: 'w00p w00p',
date: 2017-04-17T17:52:45.612Z,
username: 'test',
sid: 'BJ8g-tG0e',
__v: 1,
favorites: [ 58f4f9e5650785228016287f ],
likes: [] },
_locals: { user:
{ id: 58f4f9e5650785228016287f,
username: 'test',
firstName: 'test',
lastName: 'test',
email: 'test@test.com' } } ]
_locals
是 Express 添加到您传递给 res.render()
的对象的东西。如果你要反转你所做的函数调用,它就不会再存在了:
console.log(favorites);
res.render('favorites', favorites);
但是,主要问题是您将一个数组 (favorites
) 作为参数传递给 res.render()
,它需要一个对象。调用它的正确方法是将 favorites
作为对象值传递:
res.render('favorites', { favorites : favorites });
// Or in recent Node versions:
// res.render('favorites', { favorites });
我想做的是让用户获得他们已添加到收藏夹的 post 的列表,只需将他们的 _id 放入 post 即可收藏夹数组。
当 运行 该特定查询时,它作为一个数组返回,其中包含未命名的条目以及数组中的 _locals。
我需要更像 Posts: [ {}, {} ], _locals.
如何将其转换成那种格式?
var UserSchema = new Schema({
username : String,
firstName : String,
lastName : String,
email : String,
password : String
});
var PostSchema = new Schema({
author : { type: Schema.Types.ObjectId, ref: 'User' },
date : Date,
body : String,
username : String,
sid : String,
views : Number,
likes : [{ type: Schema.Types.ObjectId, ref: 'User' }],
favorites : [{ type: Schema.Types.ObjectId, ref: 'User' }]
});
and the get
app.get('/favorites', function(req, res) {
Post
.find({ 'favorites': '58f4f9e5650785228016287f'})
.exec(function(err, favorites) {
res.render('favorites', favorites)
console.log(favorites)
})
});
I get a response of
[ { _id: 58f4f9e96507852280162880,
author: 58f4f9e5650785228016287f,
body: 'test',
date: 2017-04-17T17:22:49.059Z,
username: 'test',
sid: 'BJ-xquGRl',
__v: 2,
favorites: [ 58f4f9e5650785228016287f ],
likes: [] },
{ _id: 58f500edd8abc7242c90d61c,
author: 58f4f9e5650785228016287f,
body: 'w00p w00p',
date: 2017-04-17T17:52:45.612Z,
username: 'test',
sid: 'BJ8g-tG0e',
__v: 1,
favorites: [ 58f4f9e5650785228016287f ],
likes: [] },
_locals: { user:
{ id: 58f4f9e5650785228016287f,
username: 'test',
firstName: 'test',
lastName: 'test',
email: 'test@test.com' } } ]
_locals
是 Express 添加到您传递给 res.render()
的对象的东西。如果你要反转你所做的函数调用,它就不会再存在了:
console.log(favorites);
res.render('favorites', favorites);
但是,主要问题是您将一个数组 (favorites
) 作为参数传递给 res.render()
,它需要一个对象。调用它的正确方法是将 favorites
作为对象值传递:
res.render('favorites', { favorites : favorites });
// Or in recent Node versions:
// res.render('favorites', { favorites });