所以我有一个使用parse.com作为后端的消息传递应用程序。当我从应用程序发送消息时,它将消息保存在Parse.com上,称为“ NewMessages”类。然后在我的云代码中,我有一个专用于该类的afterSave函数,以便当一个新对象保存到“ NewMessages”时,它选择一个随机用户将其附加到消息上,并将其保存在一个名为“收件箱”的新类中。然后,它从“ NewMessages”中删除原始消息。
因此,“ NewMessages”类应始终为空,对吗?但是当我很快发送一堆消息时,有些消息会被跳过。我该如何解决?
有没有比使用afterSave更好的方法来构造它?
function varReset(leanBody, leanSenderName, leanSenderId, randUsers){
leanBody = "";
leanSenderName = "";
leanSenderId = "";
randUsers = [];
console.log("The variables were set");
}
Parse.Cloud.afterSave("Lean", function(leanBody, leanSenderName, leanSenderId, randUsers, request) {
varReset(leanBody, leanSenderName, leanSenderId, randUsers);
var query = new Parse.Query("NewMessages");
query.first({
success: function(results){
leanBody = (results.get("MessageBody"));
leanSenderName = (results.get("senderName"));
leanSenderId = (results.get("senderId"));
getUsers(leanBody, leanSenderName, leanSenderId);
results.destroy({
success: function(results){
console.log("deleted");
}, error: function(results, error){
}
});
}, error: function(error){
}
});
});
function getUsers(leanBody, leanSenderName, leanSenderId, response){
var query = new Parse.Query(Parse.User);
query.find({
success: function(results){
var users = [];
console.log(leanBody);
console.log(leanSenderName);
//extract out user names from results
for(var i = 0; i < results.length; ++i){
users.push(results[i].id);
}
for(var i = 0; i < 3; ++i){
var rand = users[Math.floor(Math.random() * users.length)];
var index = users.indexOf(rand);
users.splice(index, 1);
randUsers.push(rand);
}
console.log("The random users are " + randUsers);
sendMessage(leanBody, leanSenderName, leanSenderId, randUsers);
}, error: function(error){
response.error("Error");
}
});
}
function sendMessage(leanBody, leanSenderName, leanSenderId, randUsers){
var Inbox = Parse.Object.extend("Inbox");
for(var i = 0; i < 3; ++i){
var inbox = new Inbox();
inbox.set("messageBody", leanBody);
inbox.set("senderName", leanSenderName);
inbox.set("senderId", leanSenderId);
inbox.set("recipientId", randUsers[i]);
console.log("leanBody = " + leanBody);
console.log("leanSenderName = " + leanSenderName);
console.log("leanSenderId = " + leanSenderId);
console.log("recipient = " + randUsers[i]);
inbox.save(null, {
success: function(inbox) {
// Execute any logic that should take place after the object is saved.
alert('New object created with objectId: ' + inbox.id);
},
error: function(inbox, error) {
// Execute any logic that should take place if the save fails.
// error is a Parse.Error with an error code and message.
alert('Failed to create new object, with error code: ' + error.message);
}
});
}
}
最佳答案
您检查过日志了吗?您可能违反资源限制(https://parse.com/docs/cloud_code_guide#functions-resource)。如果即时性并不重要,则值得研究建立一个每隔几分钟运行一次的后台作业,并处理未传递的消息。也可以将两种方法结合使用:使afterSave函数尝试立即将其传递到收件箱,而后台作业则定期拾取剩余的所有NewMessage。不是最漂亮的解决方案,但至少您有更高的可靠性。 (您必须考虑竞争条件,尽管两者可能会尝试在同一NewMessage上进行传递。)
关于您的结构更好的问题,如果两个类相同(或足够接近),是否可以仅具有Messages类?最初,“收件人”字段为null,但在beforeSave函数上分配了一个随机收件人。这可能更快更整洁。
编辑:添加第三个观察,最初是评论:
我看到您在afterSave中使用Query.first()
来查找要处理的NewMessage。可能在调用Save和运行查询之间的这段时间里会插入新的NewMessage。为什么不获取已保存的NewMessage的ID并在查询中使用它而不是first()
?query.get(request.object.id,...);
这样可以确保afterSave中的代码可以处理为其调用的NewMessage,而不是最近保存的代码。