0

I have written a query to fetch data into outVar variable from mongo db via mongoose object and when i print outVar onto console from my js file, i am getting the below data

{ _id: 5c24fad96fe8c607b7e73814,
  title: 'Movies and TV series database',
  version: '1.0',
  shows:
   [ { name: 'CSI', genre: 'Sci', lead_actor: 'William Petersen' } ] }

console.log(outVar.title) and console.log(outVar.version) yields the results Movies and TV series database and 1.0 respectively. However, when i try to print outVar.shows, i get undefined.

Please help me in understanding where i am going wrong and how i can rectify the issue.

1 Answer 1

1

The mongo response should first be stringified and and then be parsed into JSON object. The resulting JSON object should then be used in the following operations, as shown below

var outString=JSON.stringify(out[0]);
var outStringJson=JSON.parse(outString);
console.log(outStringJson.shows);
console.log(outStringJson.shows[0]);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.