Loopback-datasource-juggler: Model.embedsOne undefined

Created on 31 May 2016  Ā·  26Comments  Ā·  Source: loopbackio/loopback-datasource-juggler

Hi,

Following the duck, I can define relation in code (model/customer.js) like that :

Customer.embedsOne(Address, {
  as: 'address', // default to the relation name - address
  property: 'billingAddress' // default to addressItem
});

The problem embedsOne is undefined. Am I missing something?

bug doc stale

Most helpful comment

@crandmck Sorry I misunderstood the original issue. So this issue should be a bug. Setup relations in either model.js or boot script is fine.

However, there are MANY places that have examples of setting up--virtually every page in the relations section of the docs.

So those docs are correct. Please keep them.

All 26 comments

hi @0candy , yes, with SLC in the sandbox.

FYI, my file is model/customer.js

thank u for the answer ;)

Would you mind sharing the sandbox you have so that I can reproduce the error you're seeing? Thanks!

my pleasure, wait a minute :D

On Jun 1 2016, at 6:22 pm, Candy <[email protected]> wrote:

Would you mind sharing the sandbox you have so that I can reproduce the
error you're seeing? Thanks!

—
You are receiving this because you authored the thread.
Reply to this email directly, view it on
GitHub
, or mute the
thread
.

here it is : https://github.com/iam-merlin/loopback-sandbox
I hope it's not a dumb problem :/.
You can see a console.log(Mail.embedsOne); in server/models/mail.js

it's pretty dumb... I just try to define the relation "programaticaly"

Thank you for sharing your repo. The model itself should have been created in common/models/mail.js. The same way it should be created if you use slc loopback:model. Then you can call console.log(Mail.embedsOne); in server/boot/mail.js. The file in boot can be called anything. I just left it as mail.js

Please see the example here:
https://github.com/0candy/embedRelation

Hope that helps!

Ok, this is a way :D, thank you!

I can read in the duck that the relation can be set in the code and more exactly in model : "common/models/customer.js"

https://docs.strongloop.com/display/APIC/Embedded+models+and+relations#Embeddedmodelsandrelations-Definetherelationincode

I think the duck should be fix no (does not work too in common)?

Just a last comment, the model can be in server or common, no difference in my point of view. Server is just for non-sharing model. slc loopback:model purpose that :D.

You are right, the documentation should be updated. The embedded code should be in server/boot.
The model can be in server or common depending on which you choose.

<3, thx for all.

@0Candy Are you saying that to define model relations with JS code, you have to do it in a boot script? I thought you could do it in the model JS file (eg. common/models/foo.js)?
@superkhau Is that true?

@crandmck I discussed the same problem with chandrika before and Candy's explanation is right. In our doc I think there are a few places where we modify relations in a .js code but we didn't mention that it should not be in common/models/modelName.js:
eg. the end of the nestremoting doc: https://docs.strongloop.com/display/public/LB/Nested+queries
and the doc related in this issue.

I suggest add the following tip to doc of "Creating models":

In common/models/modeName.js we cannot get model’s relations since they are not loaded. Please do relation related operations in boot file.

So when users see the doc for modelName.js, they will be aware of that relations are not loaded yet.

@crandmck Yes, I believe that is true since the models are not loaded yet. The recommended case is to define them beforehand in the common/models/*.json files. This is something that probably needs addressing.

OK, I added a note to https://docs.strongloop.com/display/APIC/Creating+models#Creatingmodels-InmodelJavaScriptfile as @jannyHou suggested.

However, there are MANY places that have examples of setting up--virtually every page in the relations section of the docs.

Before I change them, I want to confirm what I'm doing....
@superkhau @jannyHou Please confirm:

For example, BelongsTo relations currently has this:

Alternatively, you can define a ā€œbelongsToā€ relation in code, though in general this is not recommended:
[In] common/models/order.js

Order.belongsTo(Customer, {foreignKey: ā€˜customerId’});

I'll change this to

Alternatively, you can define a ā€œbelongsToā€ relation in a boot script:
[In] server/boot/relations.js

module.exports = function(app) {
  var Customer = app.models.customer;
  var Order = app.models.order;
  Order.belongsTo(Customer, {foreignKey: ā€˜customerId’});
  ...

Thank you @crandmck I think the problem in this issue is different from the belongsTo example you mentioned.

So this issue is caused by "Using/loading relationship in common/models/modelName.js"
But the belongsTo doc you mentioned is "Setting up relationship in common/models/modelName.js"

In your case, I think you are able to setup relation in modelName.js file but not sure are we deprecating it in the future. I will confirm with Raymond tomorrow.

OK, thanks @jannyHou

I was confused by @0candy's comment above

...the embedded code should be in server/boot

The code in the doc being discussed _defines_ the embedsOne relationship, right?

Customer.embedsOne(Address, {
  as: 'address', // default to the relation name - address
  property: 'billingAddress' // default to addressItem
});

So, please clarify, thanks!

@crandmck Sorry I misunderstood the original issue. So this issue should be a bug. Setup relations in either model.js or boot script is fine.

However, there are MANY places that have examples of setting up--virtually every page in the relations section of the docs.

So those docs are correct. Please keep them.

OK, thanks ... _whew_! :-)

Based on the current code implementation, the embedded relation doesn't work in model.js file as the model has not been attached to the data source yet. As a workaround, please define the relation in model.json or in the boot script.

@0candy Is that wording ^ you want me to add to the docs?

I was just adding a comment so we know the situation for this issue.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository.

Let's add a note and change the code block captions in http://loopback.io/doc/en/lb3/Embedded-models-and-relations.html

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository.

Was this page helpful?
0 / 5 - 0 ratings