Search code examples
javascriptparse-platform

How to delete a relation of a object in Parse?


I tried several ways, but none of them worked, with destroyAll:

var currentCheckin = user.get('currentCheckin');
if(currentCheckin){
  Parse.Object.destroyAll(currentCheckin.relation('events')).
  then(function(){
     console.log('Success');
  });
}

With unset:

currentCheckin.unset('events');
currentCheckin.save(null).then(function(){
  console.log('Success');
});

What is the right way to do it? There is no much information about it out there.

Thanks


Solution

  • Well in order to delete an object in parse you have to query it first. Once you find the object you can then delete it like so.

    var currentUser = Parse.User.current().get("username");
    var Follower = Parse.Object.extend("Follow")
    var query = new Parse.Query(Follower);
    query.equalTo("follower", currentUser);
    query.equalTo("following", pic);
    query.find({
      success: function(follower) {
       for (var i = 0; i < follower.length; i++) {
       follower[i].destroy({
         success: function(follower){
           console.log("success")
          }
        })
       }
      }
    });