在一个 JavaScript 文件中解析 Cloud Code Define 和 Job

Parse Cloud Code Define and Job in one JavaScript file

我正在 运行 在上传到 Parse.com 的 main.js 文件中执行一些云代码操作。我决定在我的项目中添加一个 job,就像我 运行 在 main.js 中使用的所有其他云代码函数一样,我决定将作业写在文件。一切顺利,成功上传到服务器,除了一件事。当我去安排工作时,它给了我错误,"You need to add a job in Cloud Code before you can schedule a job." 不管怎样,在尝试了很多不同的解决方案之后,没有收到任何积极的回报,我自己上传了工作。它运作良好,我能够很好地安排时间。我在下面的代码中做错了什么导致作业和代码功能并非全部 运行 在同一个 js 文件中? 我听说我可能需要通过执行类似“var abc = require('cloud/cloudFunctions.js');”的操作将 link 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.");
        }
      });
    }); 

我试过你的代码,它工作正常。

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

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