Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

81
Views
mongoose response odd format

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: 'test@test.com' } } ]

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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 });
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!