loopback icon indicating copy to clipboard operation
loopback copied to clipboard

loopback.findModel method returns undefined

Open dmitrovskiy opened this issue 11 years ago • 4 comments

Hello. I have two models person and post. When I'm trying to get post model in person model for their events subscription I'm getting undefined value.

var Post = loopback.findModel('post');

Here is my model-config.json

...
"person": {
    "dataSource": "db",
    "public": true
  },
"post": {
    "dataSource": "db",
    "public": true
  },
...

Can you explain please why does it happen?

dmitrovskiy avatar Aug 21 '15 13:08 dmitrovskiy

As per the documentation here : https://apidocs.strongloop.com/loopback/#loopback-findmodel-1 . It is stated that it looks up a model class by name from all models created by loopback.createModel(). So, I think it doesn't work for models created by loopback cli. And hence should rather use "app.models" to get the model as described here : https://apidocs.strongloop.com/loopback/#app-models

xangy avatar Aug 25 '15 09:08 xangy

Where are you doing this lookup? All datasources and models need to be properly attached before you can run it. Hence, you cannot run it directly in the body of your models customization method.

csvan avatar Aug 29 '15 16:08 csvan

Hi @dmitrovskiy , would you please provide the information of where you write your code var Post = loopback.findModel('post'); like the path of your file or the folder contains it.

jannyHou avatar Nov 10 '15 16:11 jannyHou

I am closing it since info provided is not enough to reproduce the issue. If anyone run into similar issue or could provide more details, please feel free to reopen it:) Thanks for understanding.

jannyHou avatar Apr 05 '16 20:04 jannyHou