You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Now if I use populate with this schema, it starts to look really weird:
Product.findOne().populate('brand_id').run(function(err,product){console.log(product.brand_id);// <-- this is now a brand objectvarbrand=newBrand({_id: 'levis',name: "Levi's"});product.brand_id=brand;// <-- I'm assigning a brand object to the brand_id propertyconsole.log(product.brand_id);// <-- this is now a brand idproduct.save(function(){});});
I've created a monkey patch to resolve this situation. It restores populated references to their 'id state' and creates an accessor for the populated object. It only supports references which end in _id (e.g. brand_id).
I would be happy to integrate this into core or create a plugin for it. Whichever is more appropriate.
I have the same issue. What is the solution? It seems like this is a pretty common use case when using populate. ID -> populated object, go to save and it says, "Error: Invalid ObjectID"
Is everyone just rolling a custom solution to get around this?
I like the new
populate
functionality. It makes our code so much more readable. Our schema doesn't map well to how it works though.Consider this schema:
Now if I use populate with this schema, it starts to look really weird:
I've created a monkey patch to resolve this situation. It restores populated references to their 'id state' and creates an accessor for the populated object. It only supports references which end in
_id
(e.g.brand_id
).I would be happy to integrate this into core or create a plugin for it. Whichever is more appropriate.
Some limitations of my current approach:
_id
Further ideas for a more complete implementation:
brand: { refProp: 'brand_id' }
populate
the accessor could do an implicit fetch on first get and return a promise for the objectnull
and a separatefetch
method would be available for populating references on an initialized objectThe text was updated successfully, but these errors were encountered: