I am just starting node and am from PHP background. I have been trying to figure out promisses for some time now but unable to figure it out.
I am trying to use result of one query in 2nd query in a loop, but having TWO issues:
-ONE: typeof value shows an object; but when I console it out, it shows undefined
-TWO: my results are not getting in the right order. I am trying to use 'then()' but not able to quite figure it out. "inside promise 2" is consoled out before the loop in 'then()' 1 block
Here's what I'm doing in code:
exports.followers_temp = function(req, res)
{
db.sequelize.query('SELECT DISTINCT public."Users".id as user_id, public."Users".full_name, public."Users".profile_image_url, (SELECT COUNT(*) FROM public."Followships" WHERE public."Followships".leader_id = public."Users".id AND public."Followships".follower_id = :leader_id) AS is_followed FROM public."Users" INNER JOIN public."Followships" ON public."Users".id = public."Followships".follower_id WHERE public."Followships".leader_id = :leader_id LIMIT :limit OFFSET :offset', { replacements: { leader_id: req.params.leader_id, limit: req.params.limit, offset: req.params.offset }, type: db.sequelize.QueryTypes.SELECT}).
then(function(data){
console.log('i am here; length: ' + data.length);
return new Promise(function(resolve, reject) {
console.log('inside promise 1, should have access to data object');
console.log('before loop');
for(let i=0; i < data.length; i++){
var temp = db.sequelize.query('(SELECT COUNT(*) AS is_following FROM public."Followships" WHERE public."Followships".leader_id = ' + data[i].user_id + ' AND public."Followships".follower_id = :my_id)', { replacements: { leader_id: req.params.leader_id, my_id: req.params.my_id, limit: req.params.limit, offset: req.params.offset }, type: db.sequelize.QueryTypes.SELECT})
temp.then(function(value){
// Issue ONE
console.log('type: '+ typeof value); // it's an object
console.log('is_following: '+ value.is_following); // yet it's giving undefined
console.log('value: '+ JSON.stringify(value)); // but it prints out when I stringify it
data[i].is_followed = value.is_following;
});
}
resolve(data);
});
})
.then(function(my_array){
// Issue TWO
console.log('inside promise 2');
return res.status(200).send({
error:false,
message: my_array
});
})
.catch(function(err) {
console.log('inside promise catch');
return res.status(400).send({
error:true,
message: errorHandler.getErrorMessage(err)
});
});
};
and here is the console output:
i am here; length: 5
inside promise 1, should have
before loop
inside promise 2
type: object
is_following: undefined
value: [{"is_following":"1"}]
type: object
is_following: undefined
value: [{"is_following":"0"}]
type: object
is_following: undefined
value: [{"is_following":"1"}]
type: object
is_following: undefined
value: [{"is_following":"1"}]
type: object
is_following: undefined
value: [{"is_following":"0"}]
Please let me know -what am I doing wrong? -if you could working of promises syntax wise... would be great
Thanking you in anticipation; Please ignore rookie mistakes as I am a rookie as of now.