Javascript 节点js嵌套承诺。所有错误
我希望控制台输出是 回答:你成功了吗 回应(三)成功 回应(二)成功 但我得到的结果是 回答:你成功了吗 回应(二)成功 回应(三)成功 如果我将在Promise.all中删除GetMoreInfo函数,它将正常工作您有:Javascript 节点js嵌套承诺。所有错误,javascript,node.js,promise,es6-promise,Javascript,Node.js,Promise,Es6 Promise,我希望控制台输出是 回答:你成功了吗 回应(三)成功 回应(二)成功 但我得到的结果是 回答:你成功了吗 回应(二)成功 回应(三)成功 如果我将在Promise.all中删除GetMoreInfo函数,它将正常工作您有: exports.AllDataCounts= function(req, res) { var UserId = req.params.User_Id; Promise.all([ Model.SchemaOne.count({ User_Id:
exports.AllDataCounts= function(req, res) {
var UserId = req.params.User_Id;
Promise.all([
Model.SchemaOne.count({ User_Id: UserId }).exec(),
Model.SchemaTwo.find({ User_Id: UserId }).exec()
]).then(response_One => {
console.log('response_One Success');
var _ids = response_One[1].map(x => x._id );
const GetMoreInfo = _id => {
Promise.all([
Model.Cube_SchemaThree.count({ Cube_Id: _id }).exec(),
Model.Cube_SchemaFour.count({ Cube_Id: _id }).exec(),
]).then( response_three => {
console.log('response_three Success');
return response_three ;
}).catch(error_3 => {
console.log('error');
return Cube_Id;
});
};
Promise.all(
_ids.map(_id=> GetMoreInfo(_id))
).then(response_two => {
console.log('response_two Success');
res.send({ Status:true, Response: { Output1: response_One, Output3: response_two });
}).catch( error_two = > {
res.send({ Status: false, Error: error_two, Message: 'Some Error Occurred' });
});
}).catch( error_one = > {
res.send({ Status: false, Error: error_1, Message: 'Some Error Occurred' });
});
};
getMoreInfo
没有返回它的承诺。all
,因此当您调用时,它没有正确地包含在更大的承诺链中
const GetMoreInfo= _id => {
Promise.all([
Model.Cube_SchemaThree.count({ Cube_Id: _id }).exec(),
Model.Cube_SchemaFour.count({ Cube_Id: _id }).exec(),
]).then(
(相反,当前,.then
将立即解析,因为承诺。所有的都是在未定义的数组上调用的,这不是您想要的)
改为:
Promise.all(
_ids.map(_id=> GetMoreInfo(_id))
).then( ...
你有:
exports.AllDataCounts= function(req, res) {
var UserId = req.params.User_Id;
Promise.all([
Model.SchemaOne.count({ User_Id: UserId }).exec(),
Model.SchemaTwo.find({ User_Id: UserId }).exec()
]).then(response_One => {
console.log('response_One Success');
var _ids = response_One[1].map(x => x._id );
const GetMoreInfo = _id => {
Promise.all([
Model.Cube_SchemaThree.count({ Cube_Id: _id }).exec(),
Model.Cube_SchemaFour.count({ Cube_Id: _id }).exec(),
]).then( response_three => {
console.log('response_three Success');
return response_three ;
}).catch(error_3 => {
console.log('error');
return Cube_Id;
});
};
Promise.all(
_ids.map(_id=> GetMoreInfo(_id))
).then(response_two => {
console.log('response_two Success');
res.send({ Status:true, Response: { Output1: response_One, Output3: response_two });
}).catch( error_two = > {
res.send({ Status: false, Error: error_two, Message: 'Some Error Occurred' });
});
}).catch( error_one = > {
res.send({ Status: false, Error: error_1, Message: 'Some Error Occurred' });
});
};
getMoreInfo
没有返回它的承诺。all
,因此当您调用时,它没有正确地包含在更大的承诺链中
const GetMoreInfo= _id => {
Promise.all([
Model.Cube_SchemaThree.count({ Cube_Id: _id }).exec(),
Model.Cube_SchemaFour.count({ Cube_Id: _id }).exec(),
]).then(
(相反,当前,.then
将立即解析,因为承诺。所有的都是在未定义的数组上调用的,这不是您想要的)
改为:
Promise.all(
_ids.map(_id=> GetMoreInfo(_id))
).then( ...