gpt4 book ai didi

angular - TypeScript 错误 - 类型 '{}' 不是数组类型或字符串类型

转载 作者:搜寻专家 更新时间:2023-10-30 21:16:28 25 4
gpt4 key购买 nike

我有以下代码:

forkJoin([
this.restangular.all(this.commonService.getHospitalURI()).getList(),
this.restangular.all(this.commonService.getAgeGroupURI()).getList()
]).subscribe(responses => {

const hospital_result = responses[0];
// Error because of the line below
const agegroup_result = Array.from(responses[1]);

console.log('hospital ' + JSON.stringify(hospital_result))
console.log('agegroup ' + JSON.stringify(agegroup_result))

for (const productID_hospital of hospital_result[0]['product']) {

for (const productID_agegroup of agegroup_result) {
// Do Something here
}
}
})

我正在使用 Angular 5,执行 forkjoin 以按顺序调用响应。这部分完全没问题。但是当它试图解析 JSON 时出现错误。

我收到这个错误:

src/app/layout/insurance-list/insurance-list.component.ts(75,48): error TS2345: Argument of type '{}' is not assignable to parameter of type 'Iterable<{}>'.
Property '[Symbol.iterator]' is missing in type '{}'.

如果我改变

const agegroup_result = Array.from(responses[1]);

const agegroup_result = responses[1];

然后我会得到一个错误:

src/app/layout/insurance-list/insurance-list.component.ts(85,50): error TS2495: Type '{}' is not an array type or a string type.

我不明白为什么会出现此错误。我正在正确解析 JSON,因为它首先返回一个数组。由于此错误,我无法运行我的项目。

编辑:

这是 agegroup_result 的 JSON 链接: https://plnkr.co/edit/hzkPXqWfGmNNgHuHwAzT?p=catalogue

它被命名为 agegroup.json

编辑 2:

console.log(responses[1] instanceof Array)

以上代码返回真。 Response[1] 是一个数组。

最佳答案

我试图将“任何”类型的数据映射到特定的 DataModel 类型,但我收到了相同的错误消息。所以,为了解决这个问题,我建议你改用数组索引,我们开始吧:

更改这行代码:

const agegroup_result = Array.from(responses[1]);

收件人:

const ages = responses[1];

然后当你做“for”循环时,请使用数组索引:

for ( const i of Object.keys(ages)){
console.log(ages[i].propertyName);
}

我希望这可以帮助解决您的问题。

关于angular - TypeScript 错误 - 类型 '{}' 不是数组类型或字符串类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48851488/

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