Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

82
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda