I'm new to Promises with Parse Cloud Code and I'm having some difficulty figuring out why my inner promise is not correctly firing. I get all the way through majority of my promises, but it's not getting to the comment. Is there something I'm missing, or did I not structure this correctly?
You'll see where it breaks down in the comment, but it should get a user for each of the IDs in the array and then send the SMS message.
I based my code on the following topic: https://www.parse.com/questions/executing-query-within-promisethen-block
app.post('/send_sms_group', function(req, res) {
var to_group = req.body.to;
var from = req.body.from;
var message = req.body.message;
var Groups = Parse.Object.extend("Groups");
var query = new Parse.Query(Groups);
query.get(to_group).then(function(group){
return group;
}).then(function(group){
var groupOwner = group.get("groupOwner");
var members = group.get("members");
var promise = Parse.Promise.as();
for (i=0; i<members.length; i++)
{
var memberId = members[i];
if(memberId != groupOwner.id){
promise = promise.then(function(){
var findUser = new Parse.Query(Parse.User);
findUser.get(memberId).then(function(participant){
// Not getting here
console.log(participant);
if(participant.get('phone')){
console.log(participant.get('phone'));
if(!participant.get('isClaimed')){
console.log(participant.get('isClaimed'));
twilio_client.sendSms({
to:participant.get('phone'),
from:from,
body:message
}, function(err, responseData) {
console.log(responseData);
return responseData;
}
);
}
}
});
});
}
}
return promise;
}).then(function(){
res.json(200, {"status":"success"});
}, function(error) {
console.log(error);
res.json(400,error);
});
});