[英]Parse Cloud Code Define and Job in one JavaScript file
I am running a few cloud code actions in my main.js file which is uploaded to Parse.com. 我在main.js文件中运行了一些云代码操作,该文件已上传到Parse.com。 I decided to add a
job
to my project so, just like all the other cloud code functions I am running in the main.js, I decided to write the job at the bottom of the file. 我决定在我的项目中添加一个
job
,因此,就像我在main.js中运行的所有其他云代码功能一样,我决定将该工作写在文件的底部。 All was well, it was successfully uploaded to the server, except one thing. 一切都很好,除了一件事,它已成功上传到服务器。 When I went to schedule a job, it gave me the error, "You need to add a job in Cloud Code before you can schedule a job."
当我去计划一个工作时,它给我一个错误,“您需要先在Cloud Code中添加一个工作,然后才能计划一个工作。” Anyways, after trying a bunch of different solutions, and receiving no positive yield, I uploaded the job by itself.
无论如何,在尝试了许多不同的解决方案之后,并没有得到积极的回报,我独自上传了这份工作。 It worked perfectly and I was able to schedule just fine.
它工作得很好,我能够安排好一切。 What am I doing wrong in my code below to cause the jobs and the code functions to not all run in the same js file?
我在以下代码中做错了什么,导致作业和代码功能未全部在同一个js文件中运行? I heard I might need to link the main.js to another js file by doing something like '
var abc = require('cloud/cloudFunctions.js');
我听说我可能需要通过执行类似“
var abc = require('cloud/cloudFunctions.js');
将main.js链接到另一个js文件var abc = require('cloud/cloudFunctions.js');
'? '? Is that necessary?
那有必要吗?
Thanks 谢谢
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.");
}
});
});
I have try your code and it works fine. 我已经尝试过您的代码,并且效果很好。
You need to follow this steps: https://www.parse.com/docs/cloud_code_guide#started-setup 您需要执行以下步骤: https : //www.parse.com/docs/cloud_code_guide#started-setup
Add your code to the main.js file and deploy. 将代码添加到main.js文件并进行部署。 After that, you can schedule a job.
之后,您可以安排工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.