尝试将数据获取或发布到mySQL数据库时,在LOOPBACK中出错。我在这里想念的是什么?

时间:2019-03-05 19:35:01

标签: mysql express loopback

  

userauth.json

{
  "name": "userauth",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "id":{
      "type":"number",
      "required":true,
      "length":11,
      "mysql":
      {
        "columnName":"id",
        "dataType":"INT",
        "dataLength":11,
        "nullable":"N"
      }
    },
    "firstname":{
      "type":"string",
      "required":true,
      "length":25,
      "mysql":
      {
        "columnName":"firstname",
        "dataType":"VARCHAR",
        "dataLength":25,
        "nullable":"N"
      }
    },
    "lastname":{
      "type":"string",
      "required":true,
      "length":25,
      "mysql":
      {
        "columnName":"lastname",
        "dataType":"VARCHAR",
        "dataLength":25,
        "nullable":"N"
      }
    },
    "email":{
      "type":"string",
      "required":true,
      "length":50,
      "mysql":
      {
        "columnName":"email",
        "dataType":"VARCHAR",
        "dataLength":50,
        "nullable":"N"
      }
    },
    "password":{
      "type":"string",
      "required":true,
      "length":30,
      "mysql":
      {
        "columnName":"password",
        "dataType":"VARCHAR",
        "dataLength":30,
        "nullable":"N"
      }
    },
    "dd":{
      "type":"number",
      "required":true,
      "length":2,
      "mysql":
      {
        "columnName":"dd",
        "dataType":"INT",
        "dataLength":2,
        "nullable":"N"
      }
    },
    "mm":{
      "type":"number",
      "required":true,
      "length":2,
      "mysql":
      {
        "columnName":"mm",
        "dataType":"INT",
        "dataLength":2,
        "nullable":"N"
      }
    },
    "yyyy":{
      "type":"number",
      "required":true,
      "length":4,
      "mysql":
      {
        "columnName":"yyyy",
        "dataType":"INT",
        "dataLength":4,
        "nullable":"N"
      }
    }
  },
  "validations": [],
  "relations": {},
  "acl": [],
  "methods": {}
}
  

userauth.js

'use strict';

module.exports = function(userauth) {

};
  

model-config.json

{
  "_meta": {
    "sources": [
      "loopback/common/models",
      "loopback/server/models",
      "../common/models",
      "./models"
    ],
    "mixins": [
      "loopback/common/mixins",
      "loopback/server/mixins",
      "../common/mixins",
      "./mixins"
    ]
  },
  "User": {
    "dataSource": "db"
  },
  "AccessToken": {
    "dataSource": "db",
    "public": false
  },
  "ACL": {
    "dataSource": "db",
    "public": false
  },
  "RoleMapping": {
    "dataSource": "db",
    "public": false
  },
  "Role": {
    "dataSource": "db",
    "public": false
  },
  "userauth": {
    "dataSource": "db",
    "public": true
  }
}
  

datasource.json

{
  "db": {
    "host": "localhost",
    "port": 3306,
    "url": "",
    "database": "users",
    "password": "12121212",
    "name": "db",
    "user": "root",
    "connector": "mysql"
  }
}
  

尝试获取或发布时响应错误

> {
>       "error": {
>         "statusCode": 500,
>         "name": "Error",
>         "message": "ER_BAD_FIELD_ERROR: Unknown column 'model' in 'field list'",
>         "code": "ER_BAD_FIELD_ERROR",
>         "errno": 1054,
>         "sqlMessage": "Unknown column 'model' in 'field list'",
>         "sqlState": "42S22",
>         "index": 0,
>         "sql": "SELECT `model`,`property`,`accessType`,`permission`,`principalType`,`principalId`,`id`
> FROM `ACL` WHERE `model` IN ('userauth','*') AND `property` IN
> ('find','*') AND `accessType` IN ('READ','*') ORDER BY `id`",
  

mySQL数据库已连接。

     

我要指出的另一点是,回送正在创建自己的数据库名称“ acl”   而不使用创建模型时定义的数据库名称。

     

我有数据库名称用户,并使用确切的列创建了表acl   在userauth.json文件中命名属性名称

1 个答案:

答案 0 :(得分:0)

LoopBack实际上并未创建自己的名为“ ACL”的数据库。创建具有所需数据库详细信息的数据源后,您的应用程序正在使用该数据库。

您可以使用以下脚本根据已创建的LoopBack模型在数据库中标识和创建表。

在服务器文件夹中创建一个文件:“ createTables.js”,并添加以下代码:

var server = require('./server');
var ds = server.dataSources.mysql;
var lbTables = ['User', 'AccessToken', 'ACL', 'RoleMapping', 'Role', 'userauth'];
ds.automigrate(lbTables, function (er) {
  if (er) throw er;
  console.log('Loopback tables [' - lbTables - '] created in ', ds.adapter.name);
  ds.disconnect();
});

这将基于LoopBack模型的属性创建所有带有列的表。您可以通过移入服务器目录并运行节点createTable.js命令来运行脚本。

请参阅其文档:https://loopback.io/doc/en/lb2/Creating-database-tables-for-built-in-models.html

  

警告:如果名称自动迁移,则会删除现有表   与型号名称匹配。当包含数据的表存在时,使用   auto-update以避免数据丢失。

这实际上是用于为内置环回模型创建表的,但是您可以将其他模型(使用它们的名称)添加到lbTables数组中,脚本也将为这些模型创建数据库表。