我是Parse Cloud Code的Promises的新手,我很难弄清楚为什么我的内心承诺没有正确解雇。我完全承诺了我的大部分承诺,但它没有得到评论。有没有我遗失的东西,或者我没有正确构造这个?
您会在评论中看到它崩溃的位置,但它应该让用户获取阵列中的每个ID,然后发送短信。
我的代码基于以下主题: 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);
});
});
答案 0 :(得分:1)
我仍在尝试了解您的代码的详细信息。但我想你想:
看起来你的members
字段是一个字符串数组,而不是一个指针数组。
看看这个并试着了解发生了什么:
app.post('/send_sms_group', function(req, res) {
var to_group = req.body.to;
var from = req.body.from;
var message = req.body.message;
// Get the group
var groupQuery = new Parse.Query(Groups);
groupQuery.get(to_group)
.then(function(group) {
var owner = group.get('groupOwner'),
userIds = group.get('members'),
usersQuery = new Parse.Query(Parse.User);
// Remove the group owner
userIds = userIds.filter(function(user) {
return user !== owner.id;
});
// Get the users
usersQuery.containedIn('objectId', userIds);
return usersQuery.find();
})
.then(function(users) {
// Remove users without phone or "are claimed"
users = users.filter(function(user) {
return user.get('phone') && !user.get('isClaimed');
});
// Send SMS for each user
var smsPromises = users.map(function(user) {
return twilio_client.sendSms({
to: user.get('phone'),
from: from,
body: message
};
});
return Parse.Promise.when(smsPromises);
})
.then(function() {
res.json(200, {'status': 'success'});
}, function(err) {
console.error(err);
res.json(400, err);
});
});