Closed jannyHou closed 3 years ago
is this related? https://github.com/strongloop/loopback-next/issues/6029
Hello!
I have the same problem. Is having nested/embedded models in MongoDB somewhat of an advanced usage pattern?
Thank you, Manuel
@Fr4nZ82 I believe yes, they are caused by the same reason, As @bajtos pointed out, the problem is we shouldn't attach the embedded lb4 model to datasource. Would you be interested in submit a PR to fix it? The solution is explained in the "Additional information".
Let me close this one as a dup of https://github.com/strongloop/loopback-next/issues/6029
Steps to reproduce
Use case:
Customer
extendsEntity
Address
extendsModel
Customer
model has a property address:Address
Data is stored in mongodbCurrent Behavior
Problem: Fetch Customer entity from db using customerRepo.findById method, its address property has an id field with null value.
E.g.
Expected Behavior
Link to reproduction sandbox
Additional information
The problem is https://github.com/strongloop/loopback-datasource-juggler/blob/master/lib/model.js#L88 When the retrieved customer’s address property gets converted to an instance of Address , that line has an additional id field even it’s not defined in the Address model.
Quoted solution from Miroslav:
Actually, I think the code building the juggler model is correct: https://github.com/strongloop/loopback-next/blob/ec4c10c1d38ba80f59014ef447565894cf8b484b/packages/repository/src/repositories/legacy-juggler-bridge.ts#L176-L210 The problem is that we are attaching the embedded model to the datasource, which in turn adds a PK (id property) to the model definition: https://github.com/strongloop/loopback-next/blob/ec4c10c1d38ba80f59014ef447565894cf8b484b/packages/repository/src/repositories/legacy-juggler-bridge.ts#L211
I think the behavior should be as follows:
Related Issues
See Reporting Issues for more tips on writing good issues