gpt4 book ai didi

javascript - 如何修复 foreach forEach 不是函数

转载 作者:行者123 更新时间:2023-12-02 23:53:03 31 4
gpt4 key购买 nike

我对编程很陌生,当我执行 forEach 函数时,我的应用程序返回错误。我的controller.js中有以下代码

$scope.ajaxRequest = A.Game.get({action: 'game',id: user.id});
$scope.ajaxRequest.$promise.then(function(){
$scope.ajaxRequest.game.forEach(function(entry) {
if(cards.indexOf(entry) !== -1) {
console.log('alredy in game');
} else {
if(entry.id != user.id){
cards.push(entry);
$scope.imageLocations.push(entry.photo);
}
}
});




我的控制台给我这个错误

ionic.bundle.js:26799 TypeError: $scope.ajaxRequest.game.forEach is not a function
at controllers.js:2383
at processQueue (ionic.bundle.js:29132)
at ionic.bundle.js:29148
at Scope.$eval (ionic.bundle.js:30400)
at Scope.$digest (ionic.bundle.js:30216)
at Scope.$apply (ionic.bundle.js:30508)
at done (ionic.bundle.js:24829)
at completeRequest (ionic.bundle.js:25027)
at XMLHttpRequest.requestLoaded (ionic.bundle.js:24968)

最佳答案

这个

          $scope.ajaxRequest.$promise.then(function(){                                      
$scope.ajaxRequest.game.forEach(function(entry) {

看起来很可疑。将其更改为

          $scope.ajaxRequest.$promise.then(function(result) {                                      
result.forEach(function(entry) {

现在,在没有看到结果的情况下,我不会保证它会起作用,但这是使用异步调用和 promise 的模式:在 .then block 中,放置一个函数使用 promise 链中前一个的输出作为参数。所以至少有这个改变

          $scope.ajaxRequest.$promise.then(function(result) {                                      

(而不是 $scope.ajaxRequest.$promise.then(function() {) 是必要的。

关于javascript - 如何修复 foreach forEach 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55558008/

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