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

352
Visualizações
How to access a Javascript object property whose type is undefined?

Given I have a JS object which gets is value from a mongodb database via mongoose using this function

 const followed = await User.find(
      { _id: followie },
      {
        _id:0,"present": {
          $in: [followerid, "$followers"],
        }
      }
    );

When I console.log(followed), I get the following output

  console.log(followed[0]);
/*output : 
[nodemon] restarting due to changes...
[nodemon] starting `node index.js`
listening on port 8800 at time: 1655365344312
Mongodb Connected
{ present: true } ---------->  console.log output */

but when I try to access the "present" object within the followed dictionary, I get the type undefined that is:

 console.log(followed[0][0]);
/*output : 
[nodemon] restarting due to changes...
[nodemon] starting `node index.js`
listening on port 8800 at time: 1655365532137
Mongodb Connected
undefined   -------->  this is the console.log() */

Upon using typeof(followed[0]) I get the type as an object but when I try to get the typeof[0][0] it gives me the type as undefined. Where am I going wrong ? I have also tried followed[0].present and followed[0][present] with same results

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

are you using mongodb or mongoose did you try followed[0]["present"]

about 4 years ago · Juan Pablo Isaza Relatório

0

The problem with my approach and directly handling the .present key in the dictionary was that the mongoose data being sent was not in JSON format. The find() function returned an instance of the Mongoose Document class. The data was being returned in the ._doc key in the array within the followed object.

The solution to this was to use .lean() with the find object.

const followed = await User.find(
      { _id: followie },
      {
        _id: 0,
        present: {
          $in: [followerid, "$followers"],
        },
      }
    ).lean();

This results in a json object being returned which I am able to access with normal object.key or in this case followed[0].present or followed[0][0] code.

More details can be found here:https://mongoosejs.com/docs/populate.html

about 4 years ago · Juan Pablo Isaza 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