我只是从StrongLoop了解Loopback来创建一个简单的API。
我在制作API时遇到了一些麻烦。
这是我的配置:
model.js
--------
'use strict';
module.exports = function(Account) {
};
这是我的model.json:
{
"name": "Account",
"base": "PersistedModel",
"idInjection": false,
"options": {
"validateUpsert": true
},
"properties": {
"id": {
"type": "string",
"required": true,
"defaultFn": "uuidv4",
"postgresql": {
"columnName": "id",
"dataType": "uuid"
}
},
"code": {
"type": "string",
"required": true,
"postgresql": {
"columnName": "code",
"dataType": "character varying",
"dataLength": "50"
}
},
"name": {
"type": "string",
"required": true,
"postgresql": {
"columnName": "name",
"dataType": "text"
}
},
"type": {
"type": "string",
"required": true,
"postgresql": {
"columnName": "type",
"dataType": "character varying",
"dataLength": "50"
}
},
"isActive": {
"type": "boolean",
"required": true,
"default": true
},
"isDelete": {
"type": "boolean",
"required": true,
"default": false
},
"createdBy": {
"type": "string",
"required": true,
"defaultFn": "uuidv4",
"postgresql": {
"columnName": "createdBy",
"dataType": "uuid"
}
},
"updatedBy": {
"type": "string",
"required": true,
"defaultFn": "uuidv4",
"postgresql": {
"columnName": "updatedBy",
"dataType": "uuid"
}
},
"createdAt": {
"type": "date",
"required": true,
"defaultFn": "now"
},
"updatedAt": {
"type": "date",
"required": true,
"defaultFn": "now"
}
},
"validations": [],
"relations": {},
"acls": [],
"methods": {}
}
以下模型已成功迁移,我正在使用postgresql作为我的数据库。
问题是当Loopback创建CRUD标准API时,POST API似乎在我尝试POST方法时不起作用。当我尝试使用API资源管理器时,下面是我的json字符串:
{
"id": "123e4567-e89b-12d3-a456-426655440000",
"code": "1000",
"name": "Cash and Bank",
"type": "Assets",
"isActive": true,
"isDelete": false,
"createdBy": "123e4567-e89b-12d3-a456-426655440000",
"updatedBy": "123e4567-e89b-12d3-a456-426655440000",
"createdAt": "2017-09-01T17:00:00.000Z",
"updatedAt": "2017-09-01T17:00:00.000Z"
}
但它引发了一个错误,这是响应主体:
{
"error": {
"statusCode": 500,
"name": "error",
"message": "syntax error at end of input",
"length": 92,
"severity": "ERROR",
"code": "42601",
"position": "230",
"file": "scan.l",
"line": "1078",
"routine": "scanner_yyerror",
"stack": "error: syntax error at end of input\n at Connection.parseE (/media/andylpjr/EA7C669F7C6665F3/Project/kpost/kpost-api/node_modules/pg/lib/connection.js:567:11)\n at Connection.parseMessage (/media/andylpjr/EA7C669F7C6665F3/Project/kpost/kpost-api/node_modules/pg/lib/connection.js:391:17)\n at Socket.<anonymous> (/media/andylpjr/EA7C669F7C6665F3/Project/kpost/kpost-api/node_modules/pg/lib/connection.js:129:22)\n at emitOne (events.js:96:13)\n at Socket.emit (events.js:191:7)\n at readableAddChunk (_stream_readable.js:178:18)\n at Socket.Readable.push (_stream_readable.js:136:10)\n at TCP.onread (net.js:561:20)"
}
}
对于注释,当我尝试使用SQL查询从pgadmin插入到表时,它可以正常工作。像“插入账户价值(bla bla bla)”之类的东西。我知道这可能只是一个常见的错误,请帮忙。抱歉英文不好。
答案 0 :(得分:0)
关闭好的情况,似乎我错过了我的主键模型定义,真可惜......谢谢你们。