尝试 GET 或 POST 数据到 mySQL 数据库时在 LOOPBACK 中出错。我在这里缺少什么?
Getting error in LOOPBACK when trying to GET or POST data to mySQL db. What I am missing here?
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 模型的属性创建包含列的所有表。您可以通过在服务器目录中移动和 运行ning 节点 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 数组,脚本也会为这些模型创建数据库表。
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 模型的属性创建包含列的所有表。您可以通过在服务器目录中移动和 运行ning 节点 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 数组,脚本也会为这些模型创建数据库表。