2

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 Answers 2

1

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) {
  ...

});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer. Its better now because i get the response of save(), but Its still not working,the response say's: error - 141
1

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.

Comments

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.