Populates returns {undefined:{entity}} because MongoDB Adapter does not transform "_id" to "id" before
building the populates response.
When _get finds an array of objects it transforms the array and then params.mapping kicks in and the entity gets put into {[id]:entity} problem is the code in _get gets the id from the origDoc (which has _id) not the transformed "json" variable which has the desired "id" field.
This yields an id of undefined. so the resulting array gets squashed into {undefined:{theLastEntityFound}} rather than {[id0]:...[id1]:...}
Some debugging found that moleculer-db/src/index.js _get the "origDoc" has the untransformed " _id" when using the mongo adapter but the transformed "id" when using the db adapter.
My POC fix is below but this just gets it working. The issue is somewhere in the mongo db adapter. Maybe something is missing in one of the overridden methods, i.e. it is not transforming the ids on "populate" responses?
.then(json => {
if (_.isArray(json) && params.mapping === true) {
let res = {};
json.forEach((doc, i) => {
// origDoc is pre-transform so has _id not id
// use doc instead
// const id = origDoc[i][this.settings.idField]; <<< busted
const id = doc[this.settings.idField]; /// <<< "demo fix" problem is really elsewhere
res[id] = doc;
});
return res;
} else if (_.isObject(json) && params.mapping === true) {
let res = {};
// LIKELY this is brokedid as well, not tried
const id = origDoc[this.settings.idField];
res[id] = json;
return res;
}
return json;
});
Hello all,
Populates returns
{undefined:{entity}}
because MongoDB Adapter does not transform "_id" to "id" before building the populates response.When
_get
finds an array of objects it transforms the array and thenparams.mapping
kicks in and the entity gets put into{[id]:entity}
problem is the code in_get
gets the id from the origDoc (which has_id
) not the transformed "json" variable which has the desired "id" field.This yields an id of undefined. so the resulting array gets squashed into
{undefined:{theLastEntityFound}}
rather than{[id0]:...[id1]:...}
Some debugging found that moleculer-db/src/index.js
_get
the "origDoc" has the untransformed " _id" when using the mongo adapter but the transformed "id" when using the db adapter.My POC fix is below but this just gets it working. The issue is somewhere in the mongo db adapter. Maybe something is missing in one of the overridden methods, i.e. it is not transforming the ids on "populate" responses?
Reproducing the issue
MemoryAdapter output - Correct!
MongoDB output - Not so much!