何らかの理由で、54 行目 (星印が付けられています************) で、undefined の 'push' を呼び出すことができないというエラーが表示されます。最初は空であるはずの「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);
}
});
}
}