Reputation: 13
I have some trouble with converting string to ObjectId in loopback.js. Some times it's inserted as a string, so the relations and filters doesn't work (GUID field). Example in database :
/* 1 */
{
"_id" : ObjectId("59a3d023fe1dfe3cd6fae101"),
"GUID" : "59a3d023f82383384ea87e40",
"username" : "John Doe"
}
/* 2 */
{
"_id" : ObjectId("59a3d0f3fe1dfe3cd6fae104"),
"GUID" : ObjectId("59a3d023f82383384ea87e40"),
"username" : "John Doe"
}
The GUID field is an existing ID from an other loopback/Mongo application. And you can see it's the same GUID in the 2 documents, i runned twice the same code and the second time it worked.. The code i use :
data.user.GUID = new ObjectID(data.user.GUID);
data.project.GUID = new ObjectID(data.project.GUID);
return Promise.all([
app.models.user.upsertWithWhere({
GUID: data.user.GUID
}, {
GUID: data.user.GUID,
username: data.user.username
}),
app.models.project.upsertWithWhere({
GUID: data.project.GUID
}, {
GUID: data.project.GUID,
name: data.project.name
})
]).then((result) => {
// console.log("USER", result[0]);
// console.log("PROJECT", result[1]);
return result;
});
I really don't understand what's happening, if you can help me please ! <3
Upvotes: 0
Views: 893
Reputation: 10785
Disclaimer: I am one of LoopBack maintainers.
I am afraid LoopBack is still not able to correctly handle ObjectID
in all cases. Here are few things you can try to see if it fixes the problem you are encountering:
1) Enable strictObjectIDCoercion
flag in your model configuration (docs).
// common/models/project.json
{
"name": "Project",
// ...
"options": {
"validateUpsert": true,
"strictObjectIDCoercion": true
}
}
2) Add loopback-datatype-objectid to your application and define your GUID
properties as "type": "objectid"
. See this comment for more details.
Upvotes: 1