Reputation: 2741
What I'm trying to do is let the user get a list of posts that they have added to their favorites which is done by simply putting their _id into the post's favorites array.
when running that particular query, it comes back as an array with unnamed entries along with _locals in the array.
I need something more like Posts: [ {}, {} ], _locals.
How do I get it into that format?
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: '[email protected]' } } ]
Upvotes: 0
Views: 101
Reputation: 203304
That _locals
is something that Express adds to the object that you pass to res.render()
. If you would reverse the function calls you made, it wouldn't be there anymore:
console.log(favorites);
res.render('favorites', favorites);
However, the main issue is that you're passing an array (favorites
) as argument to res.render()
, which expects an object instead. The correct way of calling it would be by passing favorites
as an object value:
res.render('favorites', { favorites : favorites });
// Or in recent Node versions:
// res.render('favorites', { favorites });
Upvotes: 1