0

I need to update particular fields of multiple objects stored in a class. I can do this from the client, but I don't like the idea of it handling that extra bandwidth. How can I update and save objects I've iterated over in a Cloud Code query? Essentially, what's the JS equivalent of the following Swift method?

var query = PFQuery(className:"GameScore")
query.whereKey("playerName", equalTo:"Sean Plott")
query.findObjectsInBackgroundWithBlock {
  (objects: [AnyObject]?, error: NSError?) -> Void in

  if error == nil {
    // The find succeeded.
    println("Successfully retrieved \(objects!.count) scores.")
    // Do something with the found objects
    if let objects = objects as? [PFObject] {
      for object in objects {
        // Update object here
      }

      // Save your changes to ALL objects
      PFObject.saveAllInBackground()
    }
  } else {
    // Log details of the failure
    println("Error: \(error!) \(error!.userInfo!)")
  }
}

1 Answer 1

3

Im not sure exactly what you are trying to do, but perhaps this is what you are looking for:

Parse.Cloud.define("updateScores", function(request, response) {
    var query = new Parse.Query("GameScore");
    query.equalTo("playerName", "Sean Plott");
    query.each(function (object) {
        // Do something with object
        object.save();
    }).then(function (success) {
        request.success("OK");
    }, function(err) {
        request.error(err);
    });
});
Sign up to request clarification or add additional context in comments.

3 Comments

I'm trying to update multiple objects with the same value. Would I call object.set() to do the actual update? Let's say a blogger changes his username, I want to update each of his blogs with the updated username of the blogger.
Yes, you can try object.set("Score", newScore)
I'm getting [Error]: success/error was not called (Code: 141, Version: 1.7.2)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.