Reputation: 109
I have two classes - User, and GameScore. Each User has fields called [R,G,B,W,U], and when the user gets updated, I would like to update these fields for the matching user location in the GameScore class. I intend to do this by comparing city, country, and state fields, but would rather do it by matching LatLng (an array). I have tried accomplishing this with cloud code using afterSave:
Parse.Cloud.afterSave("User", function (request) {
var city = request.object.get("city");
var country = request.object.get("country");
var state = request.object.get("state");
var latLng = request.object.get("LatLng");
var GameScore = Parse.Object.extend("GameScore");
var query = new Parse.Query(GameScore);
query.equalTo("city", city);
query.find({
success: function (results) {
// check if state and country match here, else create new record
var fromParse = JSON.parse(results);
var objectId = fromParse.objectId;
console.log(objectId);
// need to add the change in User's Score to globalScore for their city here
},
error: function (error) {
console.log("Error: " + error.code + " " + error.message);
var newScore = Parse.Object.extend("GameScore");
newScore.set("R",request.object.get("R"));
newScore.set("G",request.object.get("G"));
newScore.set("B",request.object.get("B"));
newScore.set("U",request.object.get("U"));
newScore.set("W",request.object.get("W"));
newScore.set("city",city);
newScore.set("country",country);
newScore.set("state",state);
newScore.set("LatLng",latLng);
newScore.save();
}
});
});
After deploying, and triggering the afterSave hook, I get this error:
E2015-11-26T17:13:57.896Z]v11 after_save triggered for User: Input: {"object":{"B":0,"G":4,"LatLng":[47.6062095,-122.3320708],"R":6,"U":0,"W":3,"city":"Seattle","country":"US","createdAt":"2015-11-23T03:07:24.043Z","currentMonth":"Nov","number":"00000","objectId":"oC7RBcwztX","smsIds":[null],"state":"CA","textsLifetime":20,"textsThisMonth":20,"updatedAt":"2015-11-26T17:13:57.892Z"}}
Result: Uncaught SyntaxError: Unexpected end of input in :0
Parse.com! Help! Full disclosure: This is my first time using parse.com cloud-code. I'm enjoying it, but is there a way to clear the console log in parse's dashboard?
Upvotes: 1
Views: 419
Reputation: 161
As per https://parse.com/docs/cloudcode/guide#cloud-code-aftersave-triggers:
You should be passing the object for User like:
Parse.Cloud.afterSave(Parse.User, function(request) {...}
Additionally, I don't believe you need to JSON.Parse the return data and id can be accessed with the 'id' property on parse objects.
gameScore.save(null, {
success: function(gameScore) {
// Execute any logic that should take place after the object is saved.
alert('New object created with objectId: ' + gameScore.id);
},
https://parse.com/docs/js/guide#objects-saving-objects
Upvotes: 1