Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse - Check for Update inside BeforeSave in Cloud Code

I want to check that I am not saving a duplicate entry for the attend status of an event - so on BeforeSave I am checking that the event rsvp has not already been entered - if it has, I want to know if it needs to be updated. If it does, I want to do an update instead of create a new RSVP entry.

This is my code - I can't seem to get it to work, even with the simplist update inside BeforeSave.

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

var eventid = request.object.get("eventid");
var userid = request.object.get("userid");
var rsvp_status = request.object.get("rsvp_status");

var Rsvps = Parse.Object.extend("Rsvps");
var query = new Parse.Query(Rsvps);
query.equalTo("eventid", eventid);
query.equalTo("userid", userid);
query.first({
  success: function(object) {
    if (object) {
        // response.error("An RSVP for this event already exists.");
        request.object.id = object.id;
        request.object.set('rsvp_status', "attending");
        request.object.save();
    } else {
      response.success();
    }
  },
  error: function(error) {
    response.error("Error: " + error.code + " " + error.message);
  }
});

});

I've tried so many variation of this without any joy - this my latest attempt.

like image 698
Citylogic Avatar asked Nov 02 '22 06:11

Citylogic


1 Answers

@CityLogic you shouldn't have to call that second save in @ahoffer's example, because you are in the beforeSave trigger. Just set the resp_status and call response.success().

like image 69
Timan Rebel Avatar answered Nov 15 '22 05:11

Timan Rebel