I'm trying to use an afterSave function to update a field called accountBallance on a User object after something has been saved/updated to my Transactions object:
Parse.Cloud.afterSave("Transactions", function(request) {
var amount = request.object.get("amount");
var userId = request.object.get("user").id;
console.log("Amount: " + amount);
console.log("User: " + userId);
Parse.Cloud.useMasterKey();
var user = new Parse.User();
var query = new Parse.Query(Parse.User);
query.equalTo("objectId", userId);
query.first({
success: function(myObject) {
var newBallance = myObject.get("accountBallance") + amount;
myObject.set("accountBallance", newBallance);
myObject.save();
// Set the job's success status
response.success("User: " + userID + " Balance was updated with: " + amount);
},
error: function() {
// Set the job's success status
response.error("Couldn't update transaction!");
}
});
});
I'm getting the following error:
Result: TypeError: Cannot call method 'get' of undefined at query.first.success (main.js:22:38) at Parse.js:2:5786 at r (Parse.js:2:4981) at Parse.js:2:4531 at Array.forEach (native) at Object.w.each.w.forEach [as _arrayEach] (Parse.js:1:666) at n.extend.resolve (Parse.js:2:4482) at r (Parse.js:2:5117) at Parse.js:2:4531 at Array.forEach (native)
Any advice/help would be much appreciated.
It looks like the value returned from your query (myObject) is null. You should add a check for this:
if(myObject){
// We are good!
}else{
// user not found...maybe it was deleted?
}
Are you absolutely sure that the objectId printed here
console.log("User: " + userId);
Is in your user table?
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.