Search code examples
javascriptswiftparse-platformparse-serverparse-cloud-code

Parse server cloud code getting and updating an object


When a user shares a post I want to be able to give them a reward point for doing so. I am calling the cloud function from xcode like this.

PFCloud.callFunction(inBackground: "shares", withParameters: ["objectID" : "z2pU3UDFrh"])

I hardcoded an object id for now just to check if its working. Here is my cloud code function that gets called

  Parse.Cloud.define("shares", function(request, response) {

    var shareQuery = new Parse.Query("Parse.POSTS");
    shareQuery.get(request.params.objectID, {
      success: function(object) {
        console.log(object)
        object.increment("score");
        object.save();
      },
      error: function(error) { },
      useMasterKey: true
    });

  });

when I check the logs it prints "undefined" and the score remains unchanged


Solution

  • Replace var shareQuery = new Parse.Query("Parse.POSTS"); with
    var shareQuery = new Parse.Query("POSTS");

    Parse.Cloud.define("shares", function(request, response) {
    
        var shareQuery = new Parse.Query("POSTS");
        shareQuery.get(request.params.objectID, {
          success: function(object) {
            console.log(object)
            object.increment("score");
            object.save();
          },
          error: function(error) {
            console.error(error)
          },
          useMasterKey: true
        });
    
      });