尝试使用 Parse Cloud Code 销毁数据条目时跌倒

Failling when trying to destroy a data entry with Parse Cloud Code

我有以下 beforeSave 函数,如果有任何条目具有相同的 fbEventId,它应该删除一个条目。如果查询没有找到任何条目,它也应该 return 成功(要保存的新数据)。

Parse.Cloud.beforeSave("FbEvent", function(request, response) { 
  var query = new Parse.Query("FbEvent");
  query.equalTo("fbEventId", request.object.get("fbEventId"));
  query.find({
    success: function(results){
      if (results.length>0){
          var toRemove = results[0];
          console.log(toRemove);
          toRemove.destroy({
            success: function(toRemove){
              response.success();
            }, error: function(toRemove, error){
              response.error("Failed when destroying existing entry.");
            }
          });
      } else {
      response.error();}
    }, error: function(error){
      response.error("Error");
    }
  });
});

这是return以下日志:

 I2015-01-04T17:46:43.884Z] v158: before_save triggered for FbEvent
  Input: {"original":null,"update":{"locationFbId":"locationId","startDate":"2014-12-25T00:30:00-0200","fbEventId":"1590722614482003","fbEventName":"O Beco Invade a Cidade Baixa! | Open Bar de Natal"}}
  Result: Validation failed

有什么想法吗?

之所以会出现这个结果,是因为

response.error() 

正在不带任何参数地调用。你的问题是当你真的想调用 response.success() 时你调用了 response.error()。见下文:

Parse.Cloud.beforeSave("FbEvent", function(request, response) { 
  var query = new Parse.Query("FbEvent");
  query.equalTo("fbEventId", request.object.get("fbEventId"));
  query.find({
    success: function(results){
      if (results.length>0){
          // This block here will remove only the first conflicting entry, if you want to simply not save this entry, then user response.error("Entry with this id exists, canceling save");
          var toRemove = results[0];
          console.log(toRemove);
          toRemove.destroy({
            success: function(toRemove){
              response.success();
            }, error: function(toRemove, error){
              response.error("Failed when destroying existing entry.");
            }
          });
      } else {
      // There are 0 elements in the resulting array, so we do want to save, meaning we should return success
      response.success();}
    }, error: function(error){
      response.error("Error");
    }
  });
});