-1

出于某种原因,在第 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);
        }
      });
    }
  }
4

1 回答 1

1

您可以在 getUsers 函数中本地定义 randUsers,方法是使其成为参数:

function getUsers(randUsers, response, leanBody, leanSenderName, leanSenderId, randUsers){
    //blabla
    randUsers.push(rand); //randUsers in this context is the argument passed to the function
    //blabla
}

您使用以下参数调用 getUsers:
getUsers(leanBody, leanSenderId, leanSenderName, randUsers);

因此,leanBody 是 getUsers 函数中的 randUsers,您需要将其从参数中删除或在调用 getUsers 时传递它。

于 2014-10-10T19:00:15.640 回答