gpt4 book ai didi

javascript - AngularJS 先删除 ref 文档,然后再删除父文档

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:59:50 25 4
gpt4 key购买 nike

我有一个删除函数,但它的编码不正确。有时它会抛出错误,因为 game 比所有里程碑更早被删除。如何仅在成功删除所有里程碑后调用 game.$remove()?非常感谢!

$scope.remove = function(game) {
if(game) {
var milestonesToDelete = MilestonesService.query({ gameId: game._id }, function(milestonesToDelete) {
if(milestonesToDelete) {
for(var i = 0; i < milestonesToDelete.length; i++) {
milestonesToDelete[i].$remove();
}
}
});

game.$remove();

for (var j in $scope.games) {
if ($scope.games [j] === game) {
$scope.games.splice(j, 1);
}
}
} else {
$scope.game.$remove(function() {
$location.path('admin/games');
});
}
};

最佳答案

看来您只需要在里程碑查询完成后删除游戏即可。所以只需将该代码粘贴到异步回调中:

$scope.remove = function(game) {
if(game) {
var milestonesToDelete = MilestonesService.query({ gameId: game._id }, function(milestonesToDelete) {
if(milestonesToDelete) {
for(var i = 0; i < milestonesToDelete.length; i++) {
milestonesToDelete[i].$remove();
}
}
game.$remove();

for (var j in $scope.games) {
if ($scope.games [j] === game) {
$scope.games.splice(j, 1);
}
}
});

} else {
$scope.game.$remove(function() {
$location.path('admin/games');
});
}
};

关于javascript - AngularJS 先删除 ref 文档,然后再删除父文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29721522/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com