通过生成slc loopback:模型书创建新模型时, 当我尝试运行app时,出现以下错误
> Error: Model not found: Book at Function.registry.getModel > (/home/joseph/node_projects/laplace-master/enquete/node_modules/loopback/lib/registry.js:273:9) > at > /home/joseph/node_projects/laplace-master/enquete/node_modules/loopback-boot/lib/executor.js:156:28 > at Array.forEach (native) at defineModels > (/home/joseph/node_projects/laplace-master/enquete/node_modules/loopback-boot/lib/executor.js:151:23) > at setupModels > (/home/joseph/node_projects/laplace-master/enquete/node_modules/loopback-boot/lib/executor.js:140:3) > at execute > (/home/joseph/node_projects/laplace-master/enquete/node_modules/loopback-boot/lib/executor.js:28:3) > at bootLoopBackApp > (/home/joseph/node_projects/laplace-master/enquete/node_modules/loopback-boot/index.js:125:3) > at Object. > (/home/joseph/node_projects/laplace-master/enquete/server/server.coffee:24:1) > at Object. > (/home/joseph/node_projects/laplace-master/enquete/server/server.coffee:1:1) > at Module._compile (module.js:409:26) at Object.loadFile > (/usr/lib/node_modules/coffee-script/lib/coffee-script/register.js:16:19) > at Module.load > (/usr/lib/node_modules/coffee-script/lib/coffee-script/register.js:45:36) > at Function.Module._load (module.js:300:12) at Module.require > (module.js:353:17) at require (internal/module.js:12:17) at Object. > (/home/joseph/node_projects/laplace-master/enquete/handlers/conditionchecker/brand_condition.coffee:5:7) > at Object. > (/home/joseph/node_projects/laplace-master/enquete/handlers/conditionchecker/brand_condition.coffee:1:1) > at Module._compile (module.js:409:26) at Object.loadFile > (/usr/lib/node_modules/coffee-script/lib/coffee-script/register.js:16:19) > at Module.load > (/usr/lib/node_modules/coffee-script/lib/coffee-script/register.js:45:36) > at Function.Module._load (module.js:300:12) at Module.require > (module.js:353:17) at require (internal/module.js:12:17) at Object. > (/home/joseph/node_projects/laplace-master/enquete/common/models/question-group.js:13:3) at Module._compile (module.js:409:26) at Object.Module._extensions..js > (module.js:416:10) at Module.load > (/usr/lib/node_modules/coffee-script/lib/coffee-script/register.js:45:36) > at Function.Module._load (module.js:300:12) at Module.require > (module.js:353:17) at require (internal/module.js:12:17) at > /home/joseph/node_projects/laplace-master/enquete/node_modules/loopback-boot/lib/executor.js:170:20 > at Array.forEach (native) at defineModels > (/home/joseph/node_projects/laplace-master/enquete/node_modules/loopback-boot/lib/executor.js:151:23) > at setupModels > (/home/joseph/node_projects/laplace-master/enquete/node_modules/loopback-boot/lib/executor.js:140:3) > at execute > (/home/joseph/node_projects/laplace-master/enquete/node_modules/loopback-boot/lib/executor.js:28:3) > at bootLoopBackApp > (/home/joseph/node_projects/laplace-master/enquete/node_modules/loopback-boot/index.js:125:3) > at Object. > (/home/joseph/node_projects/laplace-master/enquete/server/server.coffee:24:1) > at Object. > (/home/joseph/node_projects/laplace-master/enquete/server/server.coffee:1:1) > at Module._compile (module.js:409:26) at Object.exports.run > (/usr/lib/node_modules/coffee-script/lib/coffee-script/coffee-script.js:154:23) > at compileScript > (/usr/lib/node_modules/coffee-script/lib/coffee-script/command.js:224:29) > at compilePath > (/usr/lib/node_modules/coffee-script/lib/coffee-script/command.js:174:14) > at Object.exports.run > (/usr/lib/node_modules/coffee-script/lib/coffee-script/command.js:98:20) > at Object. (/usr/lib/node_modules/coffee-script/bin/coffee:7:41) at > Module._compile (module.js:409:26) at Object.Module._extensions..js > (module.js:416:10) at Module.load (module.js:343:32) at > Function.Module._load (module.js:300:12) at Function.Module.runMain > (module.js:441:10) at startup (node.js:139:18) at node.js:968:3
这是我的模型类和配置文件。
book.js
module.exports = function(Book) {
};
book.json
{
"name": "Book",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"id": {
"type": "string",
"required": true,
"default": "1"
},
"name": {
"type": "string",
"required": true,
"default": "nil"
}
},
"validations": [],
"relations": {},
"acls": [],
"methods": {}
}
并在server / model-config.json
中{
"_meta": {
"sources": [
"../common/models",
"./models"
]
},
"User": {
"dataSource": "db"
},
"AccessToken": {
"dataSource": "mysql",
"public": false
},
"ACL": {
"dataSource": "mysql",
"public": false
},
"RoleMapping": {
"dataSource": "mysql",
"public": false
},
"Role": {
"dataSource": "mysql",
"public": false
},
"Book": {
"dataSource": "mysql",
"public": false
}
}
请帮助解决此问题。
答案 0 :(得分:0)
我相信你可能没有在数据源中初始化模型。
您需要在启动脚本中运行以下代码(例如,将代码保存到./server/boot/prepare-db.js
。为其指定您想要的名称但它必须位于./server/boot
子文件夹中。)
module.exports = function prepareDB(app, cb) {
var ds = app.dataSources.mysql;
ds.automigrate(function() {
console.log("MySQL db ready");
});
}
automigrate
函数已记录在案here。