在一个JavaScript文件中解析云代码定义和作业
我正在我上传到Parse.com的main.js文件中运行一些云代码操作。我决定在我的项目中添加一个作业,因此,就像我在main.js中运行的所有其他云代码函数一样,我决定在文件的底部编写作业。一切都很好,它被成功地上传到服务器上,除了一件事。当我去计划一个作业时,它给了我一个错误,你需要在云代码中添加一个作业,然后才能计划作业。不管怎样,在尝试了一系列不同的解决方案后,并没有收到任何积极的效果,我自己上传了这份工作。它工作得很好,我可以安排得很好。我在下面的代码中犯了什么错误,导致作业和代码函数不在同一个js文件中运行? 我听说我可能需要通过执行“var abc=require”cloud/cloudFunctions.js“;”之类的操作将main.js链接到另一个js文件?有必要吗 谢谢在一个JavaScript文件中解析云代码定义和作业,javascript,file,parse-platform,cloud,parse-cloud-code,Javascript,File,Parse Platform,Cloud,Parse Cloud Code,我正在我上传到Parse.com的main.js文件中运行一些云代码操作。我决定在我的项目中添加一个作业,因此,就像我在main.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.");
}
});
});
我已经尝试了你的代码,它工作得很好 您需要遵循以下步骤:
将代码添加到main.js文件并部署。在那之后,你可以安排一份工作 尝试清理代码,修复缩进并添加分号,跟踪bug语法。您在作业任务中多次调用response.success,在推送回调之后,您的作业停止。@Bluety修复缩进和添加分号是什么意思?我应该在哪里添加一个?我认为一个语法错误,比如在结尾行忘记了分号,可能会导致这个问题。你用jslint测试过你的代码吗?或者多次呼叫响应。成功。我更新了我的问题。。即使删除了第一个响应。成功,仍然没有任何结果。是的,谢谢,它确实有效!它不允许我加载新作业的原因,以及它说我的云代码中没有作业的原因,是因为它是一条错误消息,告诉我我的基本帐户只能有1个解析云代码。谢谢你!