不知道为什么内心的承诺没有开火
Not sure why inner promise is not firing
我是 Promises with Parse Cloud Code 的新手,我很难弄清楚为什么我的内在承诺没有正确触发。我完全兑现了我的大部分承诺,但还没有发表评论。是不是我遗漏了什么,或者我没有正确构建它?
您会在评论中看到它在哪里崩溃,但它应该为数组中的每个 ID 获取一个用户,然后发送 SMS 消息。
我的代码基于以下主题:
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);
});
});
我仍在尝试了解您的代码的详细信息。但我想你想:
- 获取用户组
- 从用户 ID 中删除群组所有者
- 获取用户
- 删除没有 phone 或 "are claimed"
的用户
- 向过滤后的列表发送短信
看起来你的 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);
});
});
我是 Promises with Parse Cloud Code 的新手,我很难弄清楚为什么我的内在承诺没有正确触发。我完全兑现了我的大部分承诺,但还没有发表评论。是不是我遗漏了什么,或者我没有正确构建它?
您会在评论中看到它在哪里崩溃,但它应该为数组中的每个 ID 获取一个用户,然后发送 SMS 消息。
我的代码基于以下主题: 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);
});
});
我仍在尝试了解您的代码的详细信息。但我想你想:
- 获取用户组
- 从用户 ID 中删除群组所有者
- 获取用户
- 删除没有 phone 或 "are claimed" 的用户
- 向过滤后的列表发送短信
看起来你的 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);
});
});