我在一个带有模块异步系列的项目中使用Sails。
执行代码时,我收到了以下警告:
“警告:承诺是在处理程序中创建的,但未从中返回”
这是我的代码:
addEntity: function (req, res){ **// LINE 11**
async.series([
function (callback) {
Entities.create({
name: req.body.entity.name,
type: req.body.entity.type.name
}).then(function (_entity) {
this.entity = _entity;
callback();
}, function (err){
res.send({message: "Issue ..."});
});
},
function (callback) { **// LINE 24**
var where = {
caracteristique: { 'like': '%'+req.body.entity.caracteristique.name+'%'}
};
Products.find(where).then(function (_pr) {
this.products = _pr;
callback();
}, function (err){
res.send({message: "Issue ..."});
});
},
function (callback) { **// LINE 35**
this.products.forEach(function (el){
delete el.id;
el.et_id = this.entity.et_id;
Products.create(el).then(function (_newPr) {
}, function(err) {
return res.send({message: "Issue ..."})
});
callback();
});
},
], function(err, results){ **// LINE 46**
if(err) return res.send({message: 'An Error occured'});
res.json({'entity': this.entity});
});
}
错误详情:
Warning: a promise was created in a handler but was not returned from it
at wrapper (/usr/local/lib/node_modules/sails/node_modules/lodash/index.js:3592:19)
at applyInOriginalCtx (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/utils/normalize.js:417:80)
at wrappedCallback (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/utils/normalize.js:320:18)
at callback.success (/usr/local/lib/node_modules/sails/node_modules/waterline/node_modules/switchback/lib/normalize.js:33:31)
at _switch (/usr/local/lib/node_modules/sails/node_modules/waterline/node_modules/switchback/lib/factory.js:58:28)
at /usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/query/dql/create.js:248:9
at /usr/local/lib/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:52:16
From previous event:
at Deferred.toPromise (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/query/deferred.js:494:61)
at Deferred.then (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/query/deferred.js:505:15)
at async.series.callback.products.forEach.el.et_id (/Users/musecz/Lab/HUB/api/controllers/EntitiesController.js:27:30)
at /usr/local/lib/node_modules/sails/node_modules/async/lib/async.js:713:13
at iterate (/usr/local/lib/node_modules/sails/node_modules/async/lib/async.js:262:13)
at /usr/local/lib/node_modules/sails/node_modules/async/lib/async.js:274:29
at /usr/local/lib/node_modules/sails/node_modules/async/lib/async.js:44:16
at /usr/local/lib/node_modules/sails/node_modules/async/lib/async.js:718:17
at /usr/local/lib/node_modules/sails/node_modules/async/lib/async.js:167:37
at Entities.create.then.res.send.message (/Users/musecz/Lab/HUB/api/controllers/EntitiesController.js:18:11)
at wrapper (/usr/local/lib/node_modules/sails/node_modules/lodash/index.js:3592:19)
at applyInOriginalCtx (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/utils/normalize.js:417:80)
at wrappedCallback (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/utils/normalize.js:320:18)
From previous event:
at Deferred.then (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/query/deferred.js:505:27)
at async.series.where.caracteristique.like (/Users/musecz/Lab/HUB/api/controllers/EntitiesController.js:16:12)
at /usr/local/lib/node_modules/sails/node_modules/async/lib/async.js:713:13
at iterate (/usr/local/lib/node_modules/sails/node_modules/async/lib/async.js:262:13)
at async.forEachOfSeries.async.eachOfSeries (/usr/local/lib/node_modules/sails/node_modules/async/lib/async.js:281:9)
at _parallel (/usr/local/lib/node_modules/sails/node_modules/async/lib/async.js:712:9)
at Object.async.series (/usr/local/lib/node_modules/sails/node_modules/async/lib/async.js:734:9)
at Object.module.exports.addEntity (/Users/musecz/Lab/HUB/api/controllers/EntitiesController.js:11:11)
at wrapper (/usr/local/lib/node_modules/sails/node_modules/lodash/index.js:3095:19)
at routeTargetFnWrapper (/usr/local/lib/node_modules/sails/lib/router/bind.js:179:5)
at callbacks (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:164:37)
at param (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:138:11)
at pass (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:145:5)
at nextRoute (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:100:7)
at callbacks (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:167:11)
at /usr/local/lib/node_modules/sails/lib/router/bind.js:187:7
at module.exports (/Users/musecz/Lab/HUB/api/policies/sessionAuth.js:17:12)
at routeTargetFnWrapper (/usr/local/lib/node_modules/sails/lib/router/bind.js:179:5)
at callbacks (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:164:37)
Unhandled rejection Error: Callback was already called.
at /usr/local/lib/node_modules/sails/node_modules/async/lib/async.js:43:36
at /usr/local/lib/node_modules/sails/node_modules/async/lib/async.js:718:17
at /usr/local/lib/node_modules/sails/node_modules/async/lib/async.js:167:37
at /Users/musecz/Lab/HUB/api/controllers/EntitiesController.js:42:11
at Array.forEach (native)
at async.series.res.send.message (/Users/musecz/Lab/HUB/api/controllers/EntitiesController.js:35:23)
at /usr/local/lib/node_modules/sails/node_modules/async/lib/async.js:713:13
at iterate (/usr/local/lib/node_modules/sails/node_modules/async/lib/async.js:262:13)
at /usr/local/lib/node_modules/sails/node_modules/async/lib/async.js:274:29
at /usr/local/lib/node_modules/sails/node_modules/async/lib/async.js:44:16
at /usr/local/lib/node_modules/sails/node_modules/async/lib/async.js:718:17
at /usr/local/lib/node_modules/sails/node_modules/async/lib/async.js:167:37
at Products.find.then.res.send.message (/Users/musecz/Lab/HUB/api/controllers/EntitiesController.js:29:11)
From previous event:
at Deferred.then (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/query/deferred.js:505:27)
at async.series.where.caracteristique.like (/Users/musecz/Lab/HUB/api/controllers/EntitiesController.js:16:12)
at /usr/local/lib/node_modules/sails/node_modules/async/lib/async.js:713:13
at iterate (/usr/local/lib/node_modules/sails/node_modules/async/lib/async.js:262:13)
at async.forEachOfSeries.async.eachOfSeries (/usr/local/lib/node_modules/sails/node_modules/async/lib/async.js:281:9)
at _parallel (/usr/local/lib/node_modules/sails/node_modules/async/lib/async.js:712:9)
at Object.async.series (/usr/local/lib/node_modules/sails/node_modules/async/lib/async.js:734:9)
at Object.module.exports.addEntity (/Users/musecz/Lab/HUB/api/controllers/EntitiesController.js:11:11)
at wrapper (/usr/local/lib/node_modules/sails/node_modules/lodash/index.js:3095:19)
at routeTargetFnWrapper (/usr/local/lib/node_modules/sails/lib/router/bind.js:179:5)
at callbacks (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:164:37)
at param (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:138:11)
at pass (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:145:5)
at nextRoute (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:100:7)
at callbacks (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:167:11)
at /usr/local/lib/node_modules/sails/lib/router/bind.js:187:7
at module.exports (/Users/musecz/Lab/HUB/api/policies/sessionAuth.js:17:12)
at routeTargetFnWrapper (/usr/local/lib/node_modules/sails/lib/router/bind.js:179:5)
at callbacks (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:164:37)
答案 0 :(得分:0)
以这种方式尝试并检查是否仍然收到错误...
addEntity: function (req, res){ // LINE 11**
async.series([
function (callback) {
Entities.create({
name: req.body.entity.name,
type: req.body.entity.type.name
}).then(function (_entity) {
this.entity = _entity;
callback();
}, function (err){
callback(err);
//res.send({message: "Issue ..."});
});
},
function (callback) { // LINE 24**
var where = {
caracteristique: { 'like': '%'+req.body.entity.caracteristique.name+'%'}
};
Products.find(where).then(function (_pr) {
this.products = _pr;
callback();
}, function (err){
callback(err);
//res.send({message: "Issue ..."});
});
},
function (callback) { // LINE 35**
this.products.forEach(function (el){
delete el.id;
el.et_id = this.entity.et_id;
Products.create(el).then(function (_newPr) {
callback();
}, function(err) {
callback(err);
//return res.send({message: "Issue ..."})
});
//callback();
});
}
], function(err, results){ // LINE 46**
if(err) return res.send({message: 'An Error occured'});
res.json({'entity': this.entity});
});
}