Javascript forEach函数中的Http请求。角2

Javascript forEach函数中的Http请求。角2,javascript,angular,asynchronous,xmlhttprequest,Javascript,Angular,Asynchronous,Xmlhttprequest,对http请求使用forEach函数时出现问题 我有一个\u watchlistements变量,它保存以下数据: [{“xid”:“DP_049908”,“name”:“t10”},{“xid”:“DP_928829”,“name”:“t13”},{“xid”:“DP_588690”,“name”:“t14”},{“xid”:“DP_891890”,“name”:“t16”},{“xid”:“DP u 693259”,“name”:“t17”} 现在,我正在制作一个功能,可以从服务器下载这些x

对http请求使用forEach函数时出现问题

我有一个
\u watchlistements
变量,它保存以下数据:

[{“xid”:“DP_049908”,“name”:“t10”},{“xid”:“DP_928829”,“name”:“t13”},{“xid”:“DP_588690”,“name”:“t14”},{“xid”:“DP_891890”,“name”:“t16”},{“xid”:“DP u 693259”,“name”:“t17”}

现在,我正在制作一个功能,可以从服务器下载这些
xid
元素的数据:

private download() {
  this._watchlistElements.forEach(v => 
  this.http.get('http://localhost:8080/getValue/' + v.xid)
  .subscribe(res => this._values = res.json()));
}
它必须为每个
v.xid
值下载数据作为对象,并将其存储在
\u values
变量中

private\u值:数组=[]

但不知何故,angular返回一个带有
v.xid
元素的错误。它没有看到那个变量。但这有点奇怪,因为当我在控制台中执行此操作时,我的意思是:将json存储在一个变量中,并在这个
v.xid
元素上使用forEach函数,一切都很好

[default]C:\Users\src\app\appBody\watchlist\watchl中出现错误 列表组件ts:51:115 类型“WatchlistComponent”上不存在属性“xid”

存在
xid
。。。但是在异步下载数据的
\u watchlistElements
内部…


我不是100%确定这个方法是正确的,但是如果你有任何想法如何修复它,请告诉我。

当你打印出_值数组时会发生什么

上面的错误是类型错误。WatchlistComponent接口看起来像什么?它是否包括
xid
属性

您可以通过重写类型来绕过类型错误,如

private download() {
  this._watchlistElements.forEach((v as any) => 
  this.http.get('http://localhost:8080/getValue/' + v.xid)
  .subscribe(res => this._values = res.json()));
}
帮助您更好地构建代码。如果你想结合许多观察结果,我会使用类似forkJoin的东西

private download():void {
  //create an array of Observables
  let el$ = _watchlistElements.map(el => {
    return this.http.get('http://localhost:8080/getValue/' + el.xid)
             .map(res: Response => <any>res.json());
  });

  //subscribe to all, and store the data, el$ is an array of Observables
  Observable.forkJoin(el$).subscribe( data => {
    this._values = data; //data will be structured as [res[0], res[1], ...]
  });
}
private download():void{
//创建一个可观察的数组
让el$=\u watchlistElements.map(el=>{
返回此.http.get('http://localhost:8080/getValue/'+el.xid)
.map(res:Response=>


相关:

打印出_值数组时会发生什么

上面的错误是类型错误。WatchlistComponent接口是什么样子的?它是否包含
xid
属性

您可以通过重写类型来绕过类型错误,如

private download() {
  this._watchlistElements.forEach((v as any) => 
  this.http.get('http://localhost:8080/getValue/' + v.xid)
  .subscribe(res => this._values = res.json()));
}
为了帮助你更好地构造你的代码,如果你想结合许多观察结果,我会使用类似forkJoin的东西

private download():void {
  //create an array of Observables
  let el$ = _watchlistElements.map(el => {
    return this.http.get('http://localhost:8080/getValue/' + el.xid)
             .map(res: Response => <any>res.json());
  });

  //subscribe to all, and store the data, el$ is an array of Observables
  Observable.forkJoin(el$).subscribe( data => {
    this._values = data; //data will be structured as [res[0], res[1], ...]
  });
}
private download():void{
//创建一个可观察的数组
让el$=\u watchlistElements.map(el=>{
返回此.http.get('http://localhost:8080/getValue/'+el.xid)
.map(res:Response=>


相关:

如果我按照您编写的方式修复代码,我将收到4个错误。
=
预期,找不到名称
,如
预期和
xid
在WatchlistComponent上不存在。您打印
\u值
数组是什么意思?您的意思是
控制台.log
?我在手表内部没有任何接口listComponent.yes,
console.log(this._values)
;是watchlist.component.ts
this.http.get()的第51行http://localhost:8080/getValue/“+v.xid)
?我打赌
控制台.log因异步请求而无法工作。关于您的问题,是的,它看起来像是您编写的。您作为编辑编写的这篇文章有点疯狂,它不起作用:(@H.Doe我添加了一个Plunker来说明该方法。如果我按照您所写的方式修复代码,我将得到4个错误。
=
预期,找不到名称
,因为
预期,并且
xid
在WatchlistComponent上不存在。打印
\u值
数组是什么意思?您的意思是
控制台.log
?我不知道保存WatchlistComponent内的任何接口。是的,
console.log(this.\u值)
;是watchlist.component.ts
this.http.get('http://localhost:8080/getValue/“+v.xid)
?我打赌
控制台.log
会因为异步请求而无法工作。关于你的问题,是的,它看起来像是你写的。你作为编辑编写的这篇文章有点疯狂,它不起作用:(@H.Doe我添加了一个Plunker来说明该方法。