I am trying to update a user object, that is not the current user via useMasterKey. However, I get the error "invalid function" when running it. "blockedFrom" is an array in the user object that stores the list of users who blocked the concerned user and I am trying to add the usernames via addUniqueObject.
Parse.Cloud.job('addBlockedFrom', function(request, status) {
var query = new Parse.Query(Parse.User);
query.equalTo("username", request.params.otherUser);
query.each(function(record) {
record.addUniqueObject("blockedFrom", request.params.username);
return record.save({useMasterKey:true});
},{useMasterKey:true}).then(function(result) {
console.log("addBlockedFrom completed.");
status.success("addBlockedFrom completed.");
}, function(error) {
console.log("Error in addBlockedFrom: " + error.code + " " + error.message);
status.error("Error in addBlockedFrom: " + error.code + " " + error.message);
});
});
The error is right, it's not a valid cloud code function. What you defined above is a Job. You need to define a cloud code function.
Replace
" Parse.Cloud.job " with " Parse.Cloud.define "
That should do the trick.
If you have parse server version >3 then your cloud code must be changed to new version.
Try this:
Parse.Cloud.define("addBlockedFrom", async (request) => {
var query = new Parse.Query(Parse.User);
query.equalTo("username", request.params.otherUser);
try{
var otherUser = await query.first();
otherUser.addUnique("blockedFrom", request.params.username);
return otherUser.save( null,{useMasterKey:true});
}catch(err){
throw err;
}
});
After replacing Parse.Cloud.job
with Parse.Cloud.define
as suggested by @TanzimChowdhury, I still had the invalid function error because of status.success
and status.error
. Status was undefined, replacing status with response
fixed it.
Working Code
Parse.Cloud.define("addBlockedFrom", function(request, response) {
var query = new Parse.Query(Parse.User);
query.equalTo("username", request.params.otherUser);
query.each(function(user) {
user.addUnique("blockedFrom", request.params.username);
return user.save( null, { useMasterKey: true });
}).then(function() {
// Set the job's success status
response.success("addBlockedFrom successfully.");
}, function(error) {
// Set the job's error status
response.error("Error addBlockedFrom.");
});
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.