typescript Angular 8:TypeError:发现不可调用@@iterator

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/56767158/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 05:50:13  来源:igfitidea点击:

Angular 8 : TypeError: Found non-callable @@iterator

angulartypescript

提问by Bhagwat Tupe

When i'am sending http getrequest then i'am getting these error TypeError: Found non-callable @@iterator,but it works in Angular 7not in Angular 8.

当我发送http get请求时,我收到这些错误TypeError: Found non-callable @@iterator,但它Angular 7不在Angular 8.

My Component file
From component.tsfile i have called service getCodes()

我的组件文件
component.ts文件中我调用了服务getCodes()

onSubmit(){
 this.service.getCodes('localhost/Aggregator/getCodes').subscribe (result=>{
 }, error =>{
    console.log(error);
 })
}

My Interceptor file
When i 'am sending request then that time i have passed these header through interceptor

我的拦截器文件
当我发送请求时,我已经通过拦截器传递了这些标头

@Injectable()
    export class Interceptor implements HttpInterceptor {
      sessionParam:any = {
        userParam: {
          'a':66,
          'b':101,
          'c':'0201',
          'd':'Y',
          'e':'Y',
          'h':'2019/02/22',
          'f':'Y',
          'g':12
        }
      }

      intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        let headers = new HttpHeaders();
        for (var val in this.sessionParam) {
          Object.keys(this.sessionParam[val]).forEach(key => {
            headers = headers.append(key,this.sessionParam[val][key]);
          });
        }
        request = request.clone({
          headers: headers
        })
        return next.handle(request);
      }
    }

My Service file

我的服务文件

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable, of } from 'rxjs';
import { delay, map } from 'rxjs/operators';

@Injectable({
  providedIn: 'root'
})
export class ProvidersService {

  constructor(private http:HttpClient) { }

  public getCodes(url, apiParam=null):Observable<any>
  {
    return this.http.get(url, {params: apiParam});
  }
}

I'am getting following error

我收到以下错误

TypeError: Found non-callable @@iterator
at HttpHeaders.applyUpdate (http.js:301)
at http.js:245
at Array.forEach (<anonymous>)
at HttpHeaders.init (http.js:241)
at HttpHeaders.forEach (http.js:339)
at Observable._subscribe (http.js:1826)
at Observable._trySubscribe (Observable.js:42)
at Observable.subscribe (Observable.js:28)
at subscribeTo.js:20
at subscribeToResult (subscribeToResult.js:7)

any solutions ?

任何解决方案?

回答by Davis

Can't comment, but I'd recommend you look at the data ending up in your headersobject. Maybe you could log the contents of headersjust before the return next.handle(request);line. You could find, for example, that one of the values provided by this.sessionParam[val][key]is not a string and a simple this.sessionParam[val][key] + ''could do the trick...

无法发表评论,但我建议您查看对象中的数据headers。也许您可以headers在该return next.handle(request);行之前记录内容。例如,您可能会发现提供的值之一this.sessionParam[val][key]不是字符串,而简单的方法this.sessionParam[val][key] + ''可以解决问题...

回答by Dabbbb.

This error occurs when you set headers non-string values. In order to solve convert all header values to string.

当您设置标头非字符串值时会发生此错误。为了解决将所有标头值转换为字符串。

回答by Felix

I have upgraded from A7 to A8 and experienced same issue.

我已经从 A7 升级到 A8 并遇到了同样的问题。

In my case the code was failing on a row where I used destructuring assignment:

在我的情况下,代码在我使用解构赋值的行中失败:

return this.fooPipe.transform(foo, ...args);

-> Error: Found non-callable @@iterator

It has helped me to change tsconfig.jsonfrom:

它帮助我改变tsconfig.json了:

"target": "es2015"

to

"target": "es5"

回答by nastya chernomorchenko

Got same error today, my solution to this was to work with iterations on Object and then create HttpHeaders with the result. Kinda ...

今天遇到了同样的错误,我的解决方案是对 Object 进行迭代,然后使用结果创建 HttpHeaders。有点...

    let headersObj = {};

    for (var val in this.sessionParam) {
      Object.keys(this.sessionParam[val]).forEach(key => {
        headersObj[key] = this.sessionParam[val][key];
      });
    }
    request = request.clone({
      headers: new HttpHeaders(headersObj)
    })

回答by Life347

All Header should be in string while using angular 8+

使用 angular 8+ 时,所有标题都应在字符串中

回答by Tasneem Ghorayeb

This is not a new solution but maybe it helps someone.

这不是一个新的解决方案,但也许它可以帮助某人。

I have faced this issue as well after upgrading to angular 8. It threw an error where I used Object.assign({},), it was working before.

升级到 angular 8 后,我也遇到了这个问题。它在我使用 Object.assign({},) 的地方抛出了一个错误,它以前工作过。

I did not want to change all my code where I was using this in favor of the spread operator because it was way too many changes.

我不想更改我使用它的所有代码以支持扩展运算符,因为它的更改太多了。

It worked when I changed the target back to es5. But I was not sure this is the right move.(I'm still not sure what could be the pros of this.)

当我将目标更改回 es5 时,它起作用了。但我不确定这是正确的举动。(我仍然不确定这样做的优点是什么。)

After a lot of searching I came upon this link https://kangax.github.io/compat-table/es6/#typescript-core-js-noteSo here it states that for Object.assign to work, I need to have

经过大量搜索后,我发现了这个链接https://kangax.github.io/compat-table/es6/#typescript-core-js-note所以这里它指出为了 Object.assign 工作,我需要有

typescript > 3.6 & core-js >= 3

打字稿 > 3.6 & core-js >= 3

Currently angular 8 requires typescript to be less than 3.6 I am guessing this is the reason of the error, so until angular 8 supports the this typescript version or angular 9 does, I guess I will be using the same approach as Felix.

目前 angular 8 要求打字稿小于 3.6 我猜这是错误的原因,所以直到 angular 8 支持这个打字稿版本或 angular 9 支持,我想我将使用与 Felix 相同的方法。