我对node.js,sails.js和javascript一般不熟悉。我正在尝试创建一个个人网站,用于在家中进行家庭医学应用程序。
在创建此sails.js应用程序的过程中,当我尝试在多对多关联中使用addToCollection函数时,我陷入了一个问题。
这是我的代码:
// dashboard.js
module.exports = {
attributes: {
owner: {
model: 'user',
unique: true
},
boxs: {
collection: 'box',
via: 'views'
}
}
};
// box.js module.exports = {
attributes: {
x: {
type: 'integer',
required: true
},
y: {
type: 'integer',
required: true
},
moduleinfo: {
model: 'device',
unique: true
},
views: {
collection: 'dashboard',
via: 'boxs'
}
}
};
当我在代码中的某个位置(在我创建一个盒子和一个仪表板之后)尝试调用addToCollection函数时,出现错误:
Dashboard.findOne({owner: req.session.User.id}).exec(function(err, dashboard){
await Dashboard.addToCollection(dashboard.id, 'boxs', box.id);
});
这是日志:
info: Starting app...
error: A hook (`controllers`) failed to load!
error: `include-all` attempted to `require(/home/benjamin/Documents/nodejs/automathomejs/api/controllers/DeviceController.js)`, but an error occurred::
Details:SyntaxError: Unexpected identifier
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:374:25)
at Object.Module._extensions..js (module.js:417:10)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
at Module.require (module.js:354:17)
at require (internal/module.js:12:17)
at /usr/local/lib/node_modules/sails/node_modules/include-all/lib/help-include-all-sync.js:271:33
at Array.forEach (native)
at _recursivelyIncludeAll (/usr/local/lib/node_modules/sails/node_modules/include-all/lib/help-include-all-sync.js:174:11)
at includeAll (/usr/local/lib/node_modules/sails/node_modules/include-all/lib/help-include-all-sync.js:292:5)
at helpBuildDictionary (/usr/local/lib/node_modules/sails/node_modules/include-all/lib/help-build-dictionary.js:43:15)
at Function.module.exports.optional (/usr/local/lib/node_modules/sails/node_modules/include-all/index.js:67:10)
at Hook.loadControllers (/usr/local/lib/node_modules/sails/lib/hooks/moduleloader/index.js:324:18)
at Hook.wrapper [as loadControllers] (/usr/local/lib/node_modules/sails/node_modules/@sailshq/lodash/lib/index.js:3250:19)
at Hook.loadAndRegisterControllers (/usr/local/lib/node_modules/sails/lib/hooks/controllers/to-load-and-register-controllers.js:33:19)
at Hook.initialize (/usr/local/lib/node_modules/sails/lib/hooks/controllers/index.js:59:12)
at Hook.wrapper [as initialize] (/usr/local/lib/node_modules/sails/node_modules/@sailshq/lodash/lib/index.js:3250:19)
at /usr/local/lib/node_modules/sails/lib/hooks/index.js:88:16
at /usr/local/lib/node_modules/sails/node_modules/async/lib/async.js:52:16
at /usr/local/lib/node_modules/sails/node_modules/async/lib/async.js:548:17
at /usr/local/lib/node_modules/sails/node_modules/async/lib/async.js:542:17
谢谢您的时间!
本杰明
答案 0 :(得分:0)
这里有很多要拆包的东西。
首先在Sails.js中出现此错误:
error: `include-all` attempted to `require(/home/benjamin/Documents/nodejs/automathomejs/api/controllers/DeviceController.js)`, but an error occurred:: Details:SyntaxError: Unexpected identifier
通常表示您在某处有错字。
您的代码在这里:
Dashboard.findOne({owner: req.session.User.id}).exec(function(err, dashboard){
await Dashboard.addToCollection(dashboard.id, 'boxs', box.id);});
第一个.exec
很旧,不应与Sails.js一起真正使用
同样,您不能同时使用.exec
和await
。
也就是说await
必须在异步函数中。
代码的组织也令人困惑。您需要先进行垂直处理,然后再进行水平处理。
以下是重新编写代码的示例:
Good
var result = await whatever();//1
var result2 = await whatever2();//2
Bad (old)
//1
whatever((err, result)=>{
//2
whatever2((err, result2)=>{
//3
})
});
更多信息
https://github.com/mikermcneil/parley/tree/bf09a3abee35446217e799d057505a2eaf593172#flow-control
答案 1 :(得分:0)
await只能在异步函数中使用。因此,您可以尝试以下操作: 在函数之前放aync。
Dashboard.findOne({owner: req.session.User.id}).exec(async function(err, dashboard){
await Dashboard.addToCollection(dashboard.id, 'boxs', box.id);
});