简体   繁体   中英

Delete specific object from Parse.com

In my cloud code I want to retrieve the first object in the "Messages" class. Then i want to grab some information from that object, send it to another class, and finally delete that object from the "Messages" class i originally pulled it from. Below is my code, however it doesn't work. How should i rework this?

Should i use a different approach than the "destroy" method such as collection.remove?

Parse.Cloud.afterSave("sendMessage", function(Parse.Message, response) {
  var body = null;
  var senderName = null;
  var senderId = null;
  var randUsers = [];

  var query = new.Parse.Query(Parse.Message);
  query.find({
    success: function(results){
      body.push(results[1].get("messageBody"));
      senderName.push(results[1].get("senderName"));
      senderId.push(results[1].get("senderId"));
      results[1].destroy({
        success: function(results[1]){
          //the first object in the class "Messages" was deleted
        }, error: function(results[1], error){
          //the first object was not deleted
        }
      });
      response.success(getUsers);
    }, error: funtion(error){
      response.error("Error");
    }

  });
});

to avoid confusion: "getUsers" is an arbitrary function call.

Duplicate question with the entry;

Query entire class vs first object in the class

However, if you want to delete a specific object you need something which uniquely identify the object. Then, one way is using the Parse object id to delete the object from class.

To delete the object via cloud, you need to use the destroy method of ParseObject. But if you have multiple objects then you can use destroyAll method. One example of ParseObject delete method on javascript API is below;

var yourClass = Parse.Object.extend("YourClass");
var query = new Parse.Query(yourClass);
query.get("yourObjectId", {
  success: function(yourObj) {
    // The object was retrieved successfully.
    yourObj.destroy({});
  },
  error: function(object, error) {
    // The object was not retrieved successfully.
    // error is a Parse.Error with an error code and description.
  }
}); 

Hope this helps, Regards.

Some changes into above :

var missingDataQuery = new Parse.Query(missingDataObj)
            missingDataQuery.equalTo('projectId',project);

            var getMissingData = missingDataQuery.find({
                  success: function(yourObj) {
                    console.log('here')
                    yourObj[0].destroy({})
                  },
                  error: function(object, error) {

                  }
            });

Here we getting object and then destroying it.

func deleteImage(imageId: String) {

    let query = PFQuery(className: "ClassName")
    query.whereKey("imageId", equalTo: "\(imageId)")

    query.findObjectsInBackground {
        (objects:[PFObject]?, error: Error?) -> Void in

        if error == nil && (objects != nil) {
            for object in objects! {
                object.deleteInBackground()
                print("object deleted")
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM