2
votes

I'm using the following code to delete the currentUser from user friend list.

result.set("DateUserList", datelistArr); 

This works, but:

result.save();

doesn't work - when checking data browser I don't see the changes.

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

response.success("Hello world!"); });

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

var userToDelete = String(request.params.currentUser);
var user = String(request.params.dateid);

var query = new Parse.Query("User");
query.equalTo("fbId", user);

query.first(
           {
               success: function (result)
               {
                   var datelistArr = result.get("DateUserList");
                       for(j = 0 ; j < datelistArr.length;j++)
                       {
                           if (datelistArr[j] == userToDelete)
                           {
                               console.log("found a match");
                               datelistArr.splice(j);
                               console.log("deleted");
                               break;
                           }
                       }

                       console.log(datelistArr);
                       result.set("DateUserList", datelistArr);

                       result.save().then(function (savedObj)
                       {
                           response.success(userToDelete + "is deleted from " + user + " friend list");

                       },
                       function (saveError)
                       {

                           response.error("Unable to create this object");

                       });

               },
               error: function () {
                   response.error("Error occured on User deletion");
               }
           });

});

2

2 Answers

1
votes

The save is asynchronous, and you're not waiting for it to finish before calling response.success(). Use this promise syntax to call success when the result is saved instead.

result.save().then(function(obj) {
  response.success(....

}, function(err) {
  ...

});
1
votes

All i needed to do is to add the following code at the top of the cloud method:

Parse.Cloud.useMasterKey();

Reference: https://parse.com/docs/cloud_code_guide

The problem occured because I needed to use useMasterKey() method in case of changes of other users data.