I have a Model as shown below and I'm trying to do a populate in a get route. (I also have a List model that is linked to this model using ref).
var listItemSchema = mongoose.Schema({
text: String,
url: String,
bought:Boolean,
boughtBy: {
type: mongoose.Schema.Types.ObjectId,
ref: "User"
}
});
I'm basically trying to get the list, get all the listItems
and then have access to the boughtBy
object. I'm currently getting all that I need expect for the boughtBy
data.
This is my populate code:
List.findById(req.params.id)
.populate({
path: "listItems",
populate: {
path: "boughtBy",
model: "User"
}
}).exec(function(err, foundList){
if(err){
//deal with error
} else {
console.log(foundList.listItems);
}
});
This gives me this data, which i can see the boughtBy
data.
{ _id: 5ab53840ca6eba087a528473,
text: '2',
url: '',
__v: 0,
boughtBy:
{ _id: 5a8f5ab0521e5009488c97d3,
username: 'nick@testing.io',
name: 'Nick',
When I do console.log(foundList.listItems.boughtBy);
I get undefined! How can i get access to this boughtBy data. I want to send it to an ejs template.
Thanks
EDIT: I updated my code with below suggestions and manage to print the boughtBy data, but still can't access it