在一个JavaScript文件中解析云代码定义和作业

时间:2015-03-26 01:41:29

标签: javascript file parse-platform cloud cloud-code

我在main.js文件中运行了一些云代码操作,上传到Parse.com。我决定在我的项目中添加一个job,就像我在main.js中运行的所有其他云代码函数一样,我决定在文件的底部编写作业。一切都很好,它成功上传到服务器,除了一件事。当我去安排工作时,它给了我错误,"你需要在安排工作之前在Cloud Code中添加一份工作。"无论如何,在尝试了一堆不同的解决方案,并且没有收到任何正收益之后,我自己上传了这份工作。它工作得很好,我能安排得很好。我在下面的代码中做错了什么导致作业和代码函数不能全部在同一个js文件中运行? 我听说我可能需要通过执行类似' var abc = require('cloud/cloudFunctions.js');'的操作将main.js链接到另一个js文件?这有必要吗?

由于

 Parse.Cloud.define("updateScore", function(request, response) {
      if (!request.user) {
        response.error("Must be signed in to call this Cloud Function.")
        return;
      }
      // Make sure to first check if this user is authorized to perform this change.
      // One way of doing so is to query an Admin role and check if the user belongs to that Role.
      // I've chosen to use a secret key. DO NOT use this method in a PUBLIC iOS app.
      if (request.params.secret != "code1234") {
        response.error("Not authorized.")
        return;    
      }

      // The rest of the function operates on the assumption that the request is *authorized*

      Parse.Cloud.useMasterKey();

      // Query for the user to be modified by objectId
      // The objectId is passed to the Cloud Function in a 
      // key named "objectId". You can search by email or
      // user id instead depending on your use case.

      var query = new Parse.Query(Parse.User);
      query.equalTo("objectId", request.params.objectId);

      // Get the first user which matches the above constraints.
      query.first({
        success: function(anotherUser) {
          // Successfully retrieved the user.
          // Modify any parameters as you see fit.
          // You can use request.params to pass specific
          // keys and values you might want to change about
          // this user.
          anotherUser.set("totalScore", request.params.score);
          anotherUser.set("WorLStreak", request.params.worlstreak); 
          anotherUser.set("WorLNumber", request.params.worlnumber);
          anotherUser.set("FirstGoalName", request.params.firstGoalName);
          anotherUser.set("WinningTeamSelection", request.params.winningTeamSelection);

          // Save the user.
          anotherUser.save(null, {
            success: function(anotherUser) {
              // The user was saved successfully.
              response.success("Successfully updated user.");
            },
            error: function(gameScore, error) {
              // The save failed.
              // error is a Parse.Error with an error code and description.
              response.error("Could not save changes to user.");
            }
          });
        },
        error: function(error) {
          response.error("Could not find user.");
        }
      });
    });

    Parse.Cloud.define("sendPushToUsers", function(request, response) {
     var message = request.params.message;
      var recipientUserId = request.params.recipientId;
       var channelId = request.params.channelId;

    if (!request.user) {
        response.error("Must be signed in to call this Cloud Function.")
        return;
      }
      // Make sure to first check if this user is authorized to perform this change.
      // One way of doing so is to query an Admin role and check if the user belongs to that Role.
      // I've chosen to use a secret key. DO NOT use this method in a PUBLIC iOS app.
      if (request.params.secret != "code4321") {
        response.error("Not authorized.")
        return;    
      }

      Parse.Cloud.useMasterKey();

      // Validate the message text.
      // For example make sure it is under 140 characters
      if (message.length > 140) {
      // Truncate and add a ...
        message = message.substring(0, 137) + "...";
      }

      // Send the push.
      // Find devices associated with the recipient user
      var pushQuery = new Parse.Query(Parse.Installation);
      pushQuery.equalTo("channels", channelId);

      // Send the push notification to results of the query
      Parse.Push.send({
        where: pushQuery,
        data: {
          alert: message
        }
      }).then(function() {
          response.success("Push was sent successfully.")
      }, function(error) {
          response.error("Push failed to send with error: " + error.message);
      });
    });


    Parse.Cloud.define("sendPushToWinningUsers", function(request, response) {
     var message = request.params.message;
      var recipientUserId = request.params.recipientId;
       var userId = request.params.userId;

    if (!request.user) {
        response.error("Must be signed in to call this Cloud Function.")
        return;
      }
      // Make sure to first check if this user is authorized to perform this change.
      // One way of doing so is to query an Admin role and check if the user belongs to that Role.
      // I've chosen to use a secret key. DO NOT use this method in a PUBLIC iOS app.
      if (request.params.secret != "codeJob1234") {
        response.error("Not authorized.")
        return;    
      }

      Parse.Cloud.useMasterKey();

      // Validate the message text.
      // For example make sure it is under 140 characters
      if (message.length > 140) {
      // Truncate and add a ...
        message = message.substring(0, 137) + "...";
      }

      // Send the push.
      // Find devices associated with the recipient user
      var pushQuery = new Parse.Query(Parse.Installation);
      pushQuery.equalTo("owner", userId);

      // Send the push notification to results of the query
      Parse.Push.send({
        where: pushQuery,
        data: {
          alert: message
        }
      }).then(function() {
          response.success("Push was sent successfully.")
      }, function(error) {
          response.error("Push failed to send with error: " + error.message);
      });
    });

    Parse.Cloud.job("gameTime", function(request, response) {
     var message = "It’s Game Time! Tune in for live scoring updates!";

      Parse.Cloud.useMasterKey();
      var pushQuery = new Parse.Query(Parse.Installation);
      pushQuery.equalTo("channels", "PreGameNotifications");

  Parse.Push.send({
    where: pushQuery,
    data: {
      alert: message
    }
  });


      var query = new Parse.Query("Score");
      // query.equalTo("objectId", “”);

      // Get the first user which matches the above constraints.
      query.first({
        success: function(anotherUser) {

          anotherUser.set("isGameTime", "YES");

          // Save the user.
          anotherUser.save(null, {
            success: function(anotherUser) {
              response.success("Successfully updated user.");
            },
            error: function(gameScore, error) {
              response.error("Could not save changes to user.");
            }
          });
        },
        error: function(error) {
          response.error("Could not find user.");
        }
      });
    }); 

1 个答案:

答案 0 :(得分:0)

我已尝试过您的代码,但效果很好。

您需要按照以下步骤操作: https://www.parse.com/docs/cloud_code_guide#started-setup

将您的代码添加到main.js文件并进行部署。之后,您可以安排工作。