简体   繁体   English

从 Parse.com 删除特定对象

[英]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?我应该使用与“销毁”方法(例如 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.为避免混淆:“getUsers”是一个任意函数调用。

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.然后,一种方法是使用 Parse 对象 id 从类中删除对象。

To delete the object via cloud, you need to use the destroy method of ParseObject.通过云删除对象,需要使用 ParseObject 的 destroy 方法。 But if you have multiple objects then you can use destroyAll method.但是如果你有多个对象,那么你可以使用 destroyAll 方法。 One example of ParseObject delete method on javascript API is below;下面是 JavaScript API 上 ParseObject 删除方法的一个示例;

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")
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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