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

351
Views
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 answers
Answer question

0

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

about 4 years ago · Juan Pablo Isaza Report

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 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!