I'm trying to access amount. I can see datas :
Schema :
var schema = mongoose.Schema({
investors : {
id : String,
amount : Number,
user_id : String,
inv_profit : Number
}
});
Command
invs2.findOne({}, function(err, data){
console.log(data)
})
Output :
{ _id: 54159a1c291df572283fa4de,
investors:
[ { inv_profit: 0,
user_id: 'userID',
amount: 1.2,
id: '1410701852660' },
{ inv_profit: 0,
user_id: 'userID',
amount: 1.2,
id: '1410701858752' } ] }
invs2.findOne({}, function(err, data){
console.log(data.investors)
})
Output:
[ { id: '1410701852660',
amount: 1.2,
user_id: 'userID',
inv_profit: 0 },
{ id: '1410701858752',
amount: 1.2,
user_id: 'userID',
inv_profit: 0 } ]
But when I trying to access data.investors[0].amount I'm getting undefined?
Even data.investors.length is returning undefined.
There is only one entry in invs2 collection.
investors should be defined as an array in your schema if it's an array of sub-docs:
var schema = mongoose.Schema({
investors : [{
id : String,
amount : Number,
user_id : String,
inv_profit : Number
}]
});