无法设置我的环回模型.eRROR:持续的模型尚未正确连接到数据源

  • 本文关键字:模型 数据源 连接 设置 eRROR loopback
  • 更新时间 :
  • 英文 :

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"
  }
}`

我能够从资源管理器中获取,放置,发布,这意味着SQL DB已正确设置,但我无法从Restraunt.js file.try.js file.t丢弃错误。"错误:无法调用restraunt.find()。查找方法尚未设置。persistedModel尚未正确连接到数据源"

除了在引导文件夹中执行代码外,还可以使用事件,在附加模型后发出。您可以在Model.js中正确编写代码,而不是在Boot文件夹中。

看起来像:

Model.once("attached", function () {})
Model = Accounts (for example).

我知道,这是一个旧话题,但也许这对别人有帮助。

尝试再次安装mySQL连接器:

npm i -S loopback-connector-mysql

看一下您的数据源。

"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必须是:

"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) */
  }

您还需要执行餐厅模型的迁移:

创建/server/boot创建迁移。

'use strict';
module.exports = function(server) {
  var mysql = server.dataSources.mysql; 
  mysql.autoupdate('restraunt');
};

您需要迁移您使用的每个模型。如果您要将它们附加到数据源。

在文档中还说您无法在模型文件中执行任何操作,因为系统(此时)尚未满载。您需要执行的任何操作都必须在/启动目录中的.js文件内部,因为系统已完全加载。您可以在远程方法中执行操作,因为系统也已加载。

相关内容

最新更新