Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/422.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Typescript属性concat不存在_Javascript_Angular_Typescript - Fatal编程技术网

Javascript Typescript属性concat不存在

Javascript Typescript属性concat不存在,javascript,angular,typescript,Javascript,Angular,Typescript,我的代码按预期工作,但Typescript显然与我所做的事情有问题,我想了解它是什么。Typescrpt正在报告以下错误: 下面是工作代码 getZones() { this.spinner.show(); this.zonesService.getZonesByUserId(this.userId) .mergeMap(zones => Observable.from(zones)) .mergeMap(zone => this.zonesService.

我的代码按预期工作,但Typescript显然与我所做的事情有问题,我想了解它是什么。Typescrpt正在报告以下错误:

下面是工作代码

getZones() {
  this.spinner.show();
  this.zonesService.getZonesByUserId(this.userId)
    .mergeMap(zones => Observable.from(zones))
    .mergeMap(zone => this.zonesService.getAssociations(zone['id'], '/team')
      .catch(() => Observable.of(null))
      .map(team => {
        if (team['team']) {
          return Object.assign({}, zone, { teamName: team['team']['name'] });
        } else {
          return zone;
        }
      })
    )
    .mergeMap(zone => this.zonesService.getAssociations(zone['id'], '/site')
      .catch(() => Observable.of(null))
      .map(site => {
        if (site['site']) {
          return Object.assign({}, zone, { siteName: site['site']['name'] });
        } else {
          return zone;
        }
      })
    )
    .reduce((zones, zone) => {
      return zones.concat(zone);
    }, [])
    .subscribe(
      _data => {
        this.zones  = _data
          .map(zone => {
            if (zone['personal']) {
              this.personalZone = zone;
              this.personalZone['name'] = 'My Personal Zone';
              this.personalZone['teamName'] = '';
            }
            return zone;
          })
          .filter(zone => {
            return zone['personal'] !== true;
          })
          .map(zone => {
            if (zone['siteName'] === undefined) {
              zone['siteName'] = 'NO_SITE_NAME';
            }
            return zone;
          });

        if (this.zones && this.personalZone !== null) {
          this.zones.unshift(this.personalZone);
        }

        this.loading = false;
        this.spinner.hide();
      },
      _error => {}
    );
};
在我将if条件添加到map方法中之后,出现了错误。在添加这些条件之前,我的代码如下:

.map(team => {
  return Object.assign({}, zone, { teamName: team['team']['name'] });
})

.map(site => {
  return Object.assign({}, zone, { siteName: site['site']['name'] });
})
这些条件是处理无法返回请求对象的服务器实例所必需的。现在Typescript似乎与代码有问题。请确保reduce函数中的zones参数是一个数组,并且确保_数据值也是一个数组。出于某种原因,Typescript认为这些是对象

控制台输出:

console.log(zones);
console.log(zone);


谢谢你的意见

数组是对象。如果您使用的是typescript,您应该添加类型注释。您写道:“确保reduce函数中的zones参数是数组…”您如何确定这一点?运行时似乎不同意。运行时认为分区不是数组,这确实很奇怪。下面的jsBin表明它应该可以工作。我做到了,这是一个数组。这是我第一次检查。我用结果的console.log的图像更新了帖子。区域数组作为第二个参数在reduce方法中传递。