Have a collection with a reference field. I query on the raw ref. field(_id=jfhe85839rhdj). Also include the referenced row. To my supprise, in doing so you loose the ref.id: it is substituted by all values from the referenced row (if you access it, you get “object Object”).
It puts me in a spot of bother (but not unsolveable). So my question is: is this by design or an oversight? And is there any way to get the _id back into the result set without other queries and object.assign´s, it´s too costly?
If you don’t use .include() the ref field contains the ref Id.
If you use .include(), it contains the ref object.
So in this case to get the ID you have to specify it.
//without include():
.then(res => {
let item = res.items[0];
let refID = item.ref;
})
//With .include():
.then(res => {
let item = res.items[0];
let refID = item.ref._id;
})
J.D., thanks, but I need to do several iterations over an array of objects (in mem). So, I will need to put it back into the array (using object.assign, you were right) to keep things tidy. Don´t want to do that. It takes time (with large result sets) and experiments with it returned strange results (info added to the array with object.assign which literally came falling out of the sky, vulnerating its integrity. Could not even pinpoint where it came from, I did not add it, vars were clean, etc).
I´ll go for plan B, switching mid-way from ref´s _id to ref.items.title.
I don’t think I fully understand what you’re trying to do. Maybe add more description (?)
@jonatandor35 I have resolved the issue in another way. BTW, the “garbage” I picked up using object.assign is nicely described here https://stackoverflow.com/questions/728360/how-do-i-correctly-clone-a-javascript-object
and here
http://blog.bogdancarpean.com/deepcopy-of-javascript-objects-and-arrays-using-lodashs-clonedeep-method/