Reputation: 581
I am trying to access the properties of a returned MongoDB (mongoose) find.
If I try to console log the whole object, I can see it all. But if I try to log a property, I get undefined
. The object is there!
function getAll () {
let d = q.defer();
User.find({}, function (err, docs) {
if (err) {
d.reject(err);
}
for(let user of docs) {
console.log(user); // This works!
console.log(user.email); // This returns undefined!
}
d.resolve();
});
return d.promise;
}
Any idea? I also tried to use JSON.parse in case it was stringified (just to make sure) but it wasn't.
UPDATE
So seems like I can access the result using user._doc.email
.
But what causes this? I don't remember having to do this before.
Upvotes: 10
Views: 6335
Reputation: 311835
If a field in your document shows up when you console.log
the whole document, but not when you directly access that field, it means the field is missing in the model's schema definition.
So add email
to the schema of User
.
Upvotes: 29