Not able to set up my loopback model.Error:Persisted model has not been correctly attached to a DataSource

1.6k views Asked by At
restraunt.json file

`{
  "name": "restraunt",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "name": {
      "type": "string",
      "required": true
    },
    "location": {
      "type": "string",
      "required": true
    }
  },
  "validations": [],
  "relations": {},
  "acls": [],
  "methods": {}
}`

restraunt.js file

`module.exports = function(Restraunt) {
    Restraunt.find({where:{id:1}}, function(data) {
        console.log(data);
    })
};`

model-config.json  file
`"restraunt": {
    "dataSource": "restrauntManagement"
  }`

datasources.json file
`{
  "db": {
    "name": "db",
    "connector": "memory"
  },
  "restrauntManagement": {
    "host": "localhost",
    "port": 0,
    "url": "",
    "database": "restraunt-management",
    "password": "restraunt-management",
    "name": "restrauntManagement",
    "user": "rohit",
    "connector": "mysql"
  }
}`

I am able to get,put,post from the explorer which means the sql db has been set up properly but i am not able to 'find' from restraunt.js file.It throws an error. "Error: Cannot call restraunt.find(). The find method has not been setup. The PersistedModel has not been correctly attached to a DataSource"

2

There are 2 answers

7
Bruno Lebtag On

Try installing mysql connector again:

npm i -S loopback-connector-mysql

Take a look at your datasources.json, because mysql's port might be wrong, default port is 3306, also you could try changing localhost to 0.0.0.0.

"restrauntManagement": {
    "host": "localhost", /* if you're using docker, you need to set it to 0.0.0.0 instead of localhost */
    "port": 0, /* default port is 3306 */
    "url": "",
    "database": "restraunt-management",
    "password": "restraunt-management",
    "name": "restrauntManagement", 
    "user": "rohit",
    "connector": "mysql"
  }

model-config.json must be:

"restraunt": {
    "dataSource": "restrauntManagement" /* this name must be the same name in datasources object key (in your case it is restrauntManagement not the connector name which is mysql) */
  }

You also need to execute the migration for restaurant model:

create migration.js at /server/boot and add this:

'use strict';

module.exports = function(server) {
  var mysql = server.dataSources.mysql; 

  mysql.autoupdate('restraunt');
};

you need to migrate every single model you'll use it. you also need to migrate the default models (ACL, AccessToken, etc...) if you're going to attach them to a datasource.

Also in the docs says you can't perform any operation inside the model.js file because the system (at that point) it is not fully loaded. Any operation you need to execute must be inside a .js file in the /boot directory because the system is completely loaded there. You can perform operations inside remote methods because the system is loaded as well.

0
Andrew Reznik On

Besides that executing code in boot folder, there's a possibility to use event, emitted after attaching the model. You can write your code right in model.js, not in boot folder.

Looks like:

Model.once("attached", function () {})

Model = Accounts (for example).

I know, this is an old topic, but maybe this helps someone else.