Loopback-next: MongoDB ObjectId and HasOneRepository

Created on 27 Jan 2020  路  1Comment  路  Source: strongloop/loopback-next

Steps to reproduce

I experiment a bug with MongoDB using code from the shopping application example. Specifically, I copied the code for Login/User authentication in my own code (with no modifications).

Remember there are two models: User and UserCredentials linked by a HasOne relation. when you create a new user you store the password in UserCredentials with:

await this.userRepository.userCredentials(savedUser.id).create({ password });

and the result in MongoDB is:

_id: ObjectId("5e2f24850570630bc9e3f558")
password: "$2a$10$/ZGqyHNotLYeKT7uSotXpuIHVMWmfEVROMKRqtIgEufqg0plibkC2"
userId:"5e2f24850570630bc9e3f557"

The problem here is when you try to get the credentials with:

await this.userCredentials(userId).get();

you get an ENTITY_NOT_FOUND error. The reason is the last line of code try to get the credentials with userId being an ObjectId, not a string. So the create function should make it an ObjectId. I checked, manually changing userId type to ObjectId make it works.

I am very confused about this behavior. What should I do?

Related Issues

https://github.com/strongloop/loopback-next/issues/3720
https://github.com/strongloop/loopback-next/issues/3456

bug

Most helpful comment

My bad, this issue is resolved, I was using loopback-connector-mongodb 4.x instead of 5.x...
Paul.

>All comments

My bad, this issue is resolved, I was using loopback-connector-mongodb 4.x instead of 5.x...
Paul.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kesavkolla picture kesavkolla  路  3Comments

mightytyphoon picture mightytyphoon  路  3Comments

mhdawson picture mhdawson  路  3Comments

shahulhameedp picture shahulhameedp  路  3Comments

half-blood-programmer picture half-blood-programmer  路  3Comments