Can't access object property of a Mongoose response

Hugo

I'm running this code on node.js

var mongoose = require('mongoose');
mongoose.model('participant',new mongoose.Schema({},{ collection : 'forumParticipant' }));
var Participant = mongoose.model('participant');
mongoose.connect('******');

Participant.find({entity_id: 0}, function (err, docs) {
   console.log(docs[0]);
   console.log(docs[0].entity_id)
});

1) The first console.log return the full document

2) The second console.log return undefinied

I don't understand why.

I need to perform something like

var participants = docs.map(function(d){return d.user_id})

How can I achieve that? What am I missing ?

Masadow

I suspect the value you are trying to get is not in your Schema but is stored in your database.

You have two solutions from there. You can either add entity_id to your Schema and Mongo will be able to bind it to the Document object you receive. This is the recommended way.

Or you can bypass mongoose Schema and access the raw document stored in the database with docs[0]._doc.entity_id. I don't recommend this solution unless you know what you're doing.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Mongoose - can't access object properties?

From Dev

How to access object property in mongoose?

From Dev

Can't access property of Vuex store object

From Dev

Can't access property inside object initializer

From Dev

can't access property of object inside directive

From Dev

Can't access object property in Laravel

From Dev

Can't access property of Stripe customer_create response

From Dev

Can't access property from POST response headers in Angular 4

From Dev

Can't access values of Laravel's JSON response object

From Dev

Why can't I directly access a property of an object literal?

From Dev

Can't access object property when callback is invoked

From Dev

Javascript: can't access object property through for..in loop

From Dev

Can't access property if object is taken from array

From Dev

javascript - can't access property of an object using its key?

From Dev

Can not add property to an object in mongoose function

From Dev

Can't access Exception property

From Dev

Can't access details of mongoose find() result

From Java

Can't access object property even though type of returns it is an object - React + Typescript

From Dev

Can't access object property even though console.log clearly shows an Object with the needed property (React Redux)

From Dev

Mongoose: How can I access a select:false property in a schema method?

From Dev

Can't access Facebook Debugger Tool response

From Dev

Alamofire can't access keys of json response

From Java

Can't access object property, even though it shows up in a console log

From Dev

Can't access object property inside Handlebars template with Node's Express

From Dev

Can't access dependency object fields in require.js - Cannot read property of undefined

From Dev

can't access eager loaded laravel data result Trying to get property of non-object

From Dev

Can't see a JSON field in GET response (mongoose)

From Dev

I can't access property on my object via Expression.Property unless the type of the fed expression is strongly-typed

From Dev

can't access module's property or method

Related Related

  1. 1

    Mongoose - can't access object properties?

  2. 2

    How to access object property in mongoose?

  3. 3

    Can't access property of Vuex store object

  4. 4

    Can't access property inside object initializer

  5. 5

    can't access property of object inside directive

  6. 6

    Can't access object property in Laravel

  7. 7

    Can't access property of Stripe customer_create response

  8. 8

    Can't access property from POST response headers in Angular 4

  9. 9

    Can't access values of Laravel's JSON response object

  10. 10

    Why can't I directly access a property of an object literal?

  11. 11

    Can't access object property when callback is invoked

  12. 12

    Javascript: can't access object property through for..in loop

  13. 13

    Can't access property if object is taken from array

  14. 14

    javascript - can't access property of an object using its key?

  15. 15

    Can not add property to an object in mongoose function

  16. 16

    Can't access Exception property

  17. 17

    Can't access details of mongoose find() result

  18. 18

    Can't access object property even though type of returns it is an object - React + Typescript

  19. 19

    Can't access object property even though console.log clearly shows an Object with the needed property (React Redux)

  20. 20

    Mongoose: How can I access a select:false property in a schema method?

  21. 21

    Can't access Facebook Debugger Tool response

  22. 22

    Alamofire can't access keys of json response

  23. 23

    Can't access object property, even though it shows up in a console log

  24. 24

    Can't access object property inside Handlebars template with Node's Express

  25. 25

    Can't access dependency object fields in require.js - Cannot read property of undefined

  26. 26

    can't access eager loaded laravel data result Trying to get property of non-object

  27. 27

    Can't see a JSON field in GET response (mongoose)

  28. 28

    I can't access property on my object via Expression.Property unless the type of the fed expression is strongly-typed

  29. 29

    can't access module's property or method

HotTag

Archive