2

当标签被删除时,我正在使用 Parse 云代码清除所有指向“标签”对象的指针。“beforeDelete”被调用,我没有得到任何错误+控制台打印正确的“歌曲”对​​象”,其中包含指向已删除“标签”的指针。

但是指针不会从数组中删除。

Parse.Cloud.beforeDelete("Tag", function(request, response) {
    var query = new Parse.Query("Song");
    query.equalTo("tags", request.object);
    query.include(["tags"]);
     query.find({
           success: function(songs) {
              console.log(songs);
              for (var i = 0; i < songs.length; i++) {
                    songs[i].remove("tags",request.object);
                    songs[i].save();
              };
             response.success();

          },
          error: function(error) {
              response.error(error);
          }
     });
});

我究竟做错了什么 ?

谢谢沙妮

4

1 回答 1

6

object.save();是异步的。您response.success()在保存操作完成之前调用。您可以使用query.each()

var query = new Parse.Query("Song");
query.equalTo("tags", request.object);
query.include("tags");
query.each(function (song) {
  song.remove("tags",request.object);
  song.save();      
}).then(function() {
  response.success();
}, function(error) {
  response.error(error);
});

或者您可以使用并行承诺

var _ = require("underscore");

var query = new Parse.Query("Song");
query.equalTo("tags", request.object);
query.include("tags");
query.find().then(function(songs) {
  var promises = [];
  _.each(songs, function(song) {
    song.remove("tags",request.object);
    promises.push(song.save());
  });
  // Return a new promise that is resolved when all of the saves are finished.
  return Parse.Promise.when(promises);

}).then(function() {
  response.success();
}, function(error) {
  response.error(error);
});
于 2014-10-13T09:35:50.353 回答