mysql - 尝试将数据 GET 或 POST 到 mySQL 数据库时,LOOPBACK 中出现错误。我在这里缺少什么?

标签 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 IN RESPONSE WHEN TRYING TO GET or POST

> {
>       "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 db is already connected.

another point I noted that loopback is creating own db name as "acl" and not using the db name defined while creating the model.

I have db name users, and created table acl with the exact column names properties name in userauth.json file

最佳答案

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 模型的属性创建包含列的所有表。您可以通过移入服务器目录并运行 node createTable.js 命令来运行脚本。

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

Warning: Auto-migration will drop an existing table if its name matches a model name. When tables with data exist, use auto-update to avoid data loss.

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

关于mysql - 尝试将数据 GET 或 POST 到 mySQL 数据库时,LOOPBACK 中出现错误。我在这里缺少什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55010264/

相关文章:

javascript - 如何在javascript中实现环回CURL?

php - 在 phpmyadmin 中工作的 Laravel 中的 MySQL 访问冲突

java - 在 mysql 表中保存一个字符串数组

java - 环回连接失败

javascript - 使用 mongoose 查询 mongodb 集合中的正则表达式

javascript - Node express : Unrecognized content-type

node.js - Swagger:跳过未知类型 "dateTime"

mysql - Enable_broker 在 mysql 5.1 或 5.6 中不起作用

PHP数据库未添加数据

javascript - 如何根据 URI 是否在生产环境中将其设置为变量