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' });
});
};
i will expect the console output is
response_One Success
response_three Success
response_Two Success
but i got the result is
response_One Success
response_Two Success
response_Three Success
if i will remove the GetMoreInfo function inside Promise.all it works properly
You have:
const GetMoreInfo= _id => {
Promise.all([
Model.Cube_SchemaThree.count({ Cube_Id: _id }).exec(),
Model.Cube_SchemaFour.count({ Cube_Id: _id }).exec(),
]).then(
getMoreInfo
isn't returning its Promise.all
, so it's not being properly included in the larger promise chain when you call
Promise.all(
_ids.map(_id=> GetMoreInfo(_id))
).then( ...
(rather, currently, the .then
there will resolve immediately because the Promise.all
is being called on an array of undefined
s, which is not what you want)
Change to:
const GetMoreInfo= _id => {
return Promise.all([
// ...