I am running a few cloud code actions in my main.js file which is uploaded to 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. 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." 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? I heard I might need to link the main.js to another js file by doing something like '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
}
}).then(function() {
response.success("Push was sent successfully.")
}, function(error) {
response.error("Push failed to send with error: " + error.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.");
}
});
});
Aucun commentaire:
Enregistrer un commentaire