出于某种原因,在第 54 行(已加星标************)弹出一个错误,说不能调用未定义的“推送”。我最初应该为空的“randUsers”数组没有正确传递。为什么 randUsers 不能正确通过?
Parse.com 上的此云代码由 afterSave 函数触发。我声明了这些变量,因此它们将是全局变量,但是如果这是云代码而不是常规实现文件这一事实会影响到这一点,请注意。
var leanBody = "";
var leanSenderName = "";
var leanSenderId = "";
var randUsers = [];
function varReset(leanBody, leanSenderName, leanSenderId, randUsers){
leanBody = "";
leanSenderName = "";
leanSenderId = "";
randUsers = [];
console.log("The variables were set");
}
Parse.Cloud.afterSave("Lean", function(request, leanBody, leanSenderName, leanSenderId, randUsers) {
varReset(leanBody, leanSenderName, leanSenderId, randUsers);
var leanQuery = new Parse.Query("Lean");
leanQuery.first({
success: function(results){
console.log("The first object was retrieved");
leanBody = (results.get("messageBody"));
leanSenderName = (results.get("senderName"));
leanSenderId = (results.get("senderId"));
getUsers(leanBody, leanSenderId, leanSenderName, randUsers);
results.destroy({
success: function(results){
console.log("deleted");
}, error: function(results, error){
}
});
}, error: function(error){
}
});
});
function getUsers(randUsers, response, leanBody, leanSenderName, leanSenderId, randUsers){
var query = new Parse.Query(Parse.User);
query.find({
success: function(results, leanBody, leanSenderName, leanSenderId, randUsers){
var users = [];
console.log("results = " + results);
//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("rand = " + rand);
console.log("The random users are " + randUsers);
response.success(sendLean(leanBody, leanSenderId, leanSenderName));
}, error: function(error){
response.error("Error");
}
});
}
function sendLean(randUsers, leanBody, leanSenderName, leanSenderId){
var Mission = Parse.Object.extend("Mission");
var mission = new Mission();
for(var i = 0; i < 3; ++i){
mission.set("messageBody", leanBody);
mission.set("recipientId", randUsers[i]);
mission.set("senderName", leanSenderName);
mission.set("senderId", leanSenderId);
mission.save(null, {
success: function(mission) {
// Execute any logic that should take place after the object is saved.
alert('New object created with objectId: ' + mission.id);
},
error: function(mission, 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);
}
});
}
}