3

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

NReyn
  • 45
  • 5
  • Are you sure your schema is good? Is your `listItems[]` entry supposed to have an itemId or something, _and_ boughtBy, the entry itself is an ObjectId which your populate then fetches, and it incidentally has boughtBy as it's own property_ – Zlatko Mar 19 '18 at 20:29
  • I’m quite a newbie coder so not sure if it’s right, but it works so far (except for not getting “boughtBy”). Yes listItems has an ID because I use this for editing, updating and deleting them, as welll as pushing them into an array of items that a user has bought. BoughtBy isn’t actually supposed to have an ID, rather have a user ID that bought the item. – NReyn Mar 19 '18 at 20:51
  • Ok i've removed that `boughtItem` from the list schema because it is in my `ListItem` schema..think there was some duplication there. However the above code still doesn't work. It still shows as `boughtBy: [Object] }` – NReyn Mar 19 '18 at 21:23
  • Try leaving the path for the second "populate" as just "boughtBy" and see if that works. – JSilv Mar 19 '18 at 21:55
  • Thanks JSilv - tried that and got an error: `message: 'Cast to ObjectId failed for value "{ id: 5ab02913189fef15036c93c4, name: \'Mike test\' }" at path "_id" for model "User"', name: 'CastError', stringValue: '"{ id: 5ab02913189fef15036c93c4, name: \'Mike test\' }"', kind: 'ObjectId', value: { id: 5ab02913189fef15036c93c4, name: 'Mike test' }, path: '_id', reason: undefined` – NReyn Mar 19 '18 at 22:22
  • 1
    Maybe something in your schema somewhere has `id` instead of `_id`? – JSilv Mar 20 '18 at 18:13
  • Ah I think that might be it! My Listitems schema is: ````var listItemSchema = mongoose.Schema({ text: String, url: String, bought:Boolean, boughtBy: { name: String, initials: String, id: { type: mongoose.Schema.Types.ObjectId, ref: "User" } } });```` do i just change the `id` here in boughtBy to `_id`? Thanks – NReyn Mar 21 '18 at 18:34

0 Answers0