0

I am using Parse for my backend and want to search for existing friend requests and update those instead of creating new ones (if there is already an existing one).

I thought I figured out how to do it but when I submit new friend requests they get created as new objects instead of updating the old one, even though I found an existing request.

Here is the code I am using:

Parse.Cloud.beforeSave("FriendRequest", function(request, response) {

    //search for an existing friend request with the same "from" and "to" 
    var query = new Parse.Query("FriendRequest");
        query.equalTo("from", request.object.get("from"))
        .equalTo("to", request.object.get("to"));

    query.find({
      success: function(results) {
        if(results.length > 0)
        {
         var result = results[0];

         //the new request id is undefined as expected
         console.log("request id: " + request.object.id);

         //the result id is valid for an object in the db as expected
         console.log("result id: " + results[0].id);

         //set the id of the request to the id of the existing db object
         request.object.id = results[0].id;

         //the valid id is now in the request object id
         console.log("request id: " + request.object.id);

             //after response.success, the database shows a new entry
             //with a different id
         //instead of updating the existing entry
         response.success();
         }
         }
    });

});

There isn't a lot going on here. The query does come back successful with the correct entry in the database. I can confirm that I get the correct objectId for the existing item in the database. Any thoughts or ideas are appreciated!

1 Answer 1

1

You can't manually set the objectId of an object.

If you want beforeSave to NOT create a new object (which is what you're about to do when beforeSave is called), you need to manually update the existing object and then respond with a failure. If you respond with response.success(), the object will be saved normally.

In your code, you don't seem to make any changes to the existing object. All you really need to do is to return response.error (https://parse.com/docs/cloud_code_guide#functions-onsave)

Of course, you should also handle this in your code somehow. Either by alerting the user, or handling it silently.

However; why does your code attempt to save a new friend request if one already exist? Your app should know that one exists and disable the friend request button or whatever the UI offers.

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

3 Comments

Hi! Thank you for the comment. Well, I got the idea to set the objectId from the Parse forums, in an post over a year old but it was from one of their employees so I figured it would work. The code I posted has had a lot of things removed because I was trying to figure out why it wasn't working, so I wanted to eliminate unnecessary things. What I am really wanting to do is update the status of the request (for example, accepted or denied). So the request sent in is actually different than the one I query for.
Hmm, maybe I am wrong on the manually set objectId part. Do you have a link to that post?
Oh, it looks like you are right. I got the idea from this post: parse.com/questions/how-can-i-upsert But the comment where they tried to set the objectId was not from a Parse employee.

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.