简体   繁体   English

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

[英]TypeScript Error - Type '{}' is not an array type or a string type

I have the following code: 我有以下代码:

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
        }
    }
  })

I'm using Angular 5, doing a forkjoin to call a response sequentially. 我正在使用Angular 5,使用forkjoin按顺序调用响应。 This part is completely fine. 这部分完全没问题。 But the error comes when it is trying to parse the JSON. 但是当它试图解析JSON时会出现错误。

I get this error: 我收到此错误:

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 '{}'.

If I change 如果我改变

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

to

const agegroup_result = responses[1];

Then I'll get an error: 然后我会收到一个错误:

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

I don't understand why I am having this error. 我不明白为什么我有这个错误。 I am parsing the JSON correctly as it returns an array first. 我正在解析JSON,因为它首先返回一个数组。 I am unable to run my project due to this error. 由于此错误,我无法运行我的项目。

Edit: 编辑:

Here is the link to the JSON for agegroup_result: https://plnkr.co/edit/hzkPXqWfGmNNgHuHwAzT?p=catalogue 以下是agegroup_result的JSON链接: https ://plnkr.co/edit/hzkPXqWfGmNNgHuHwAzT p = catalogue

It's named as agegroup.json 它被命名为agegroup.json

Edit 2: 编辑2:

console.log(responses[1] instanceof Array)

The above code returns true. 上面的代码返回true。 Response[1] is an Array. 响应[1]是一个数组。

I tried to map my "any" type of data to a specific DataModel type, and I got the same error message. 我试图将我的“任何”类型的数据映射到特定的DataModel类型,我得到了相同的错误消息。 so, to fix the problem I suggest you use index of array instead, here we go: 所以,为了解决这个问题,我建议你使用数组的索引,这里我们去:

Change this line of code: 更改此行代码:

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

To: 至:

const ages = responses[1];

Then when you do "for" loop, please use index of array: 然后当你做“for”循环时,请使用数组的索引:

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

I hope this can help to fix your problem. 我希望这有助于解决您的问题。

its an object, not an array. 它是一个对象,而不是一个数组。 try and get all the keys from the object using the Object.Keys functions. 尝试使用Object.Keys函数从对象获取所有键。

const agegroup_result = Object.keys(responses);

then you can iterate over the keys of the responses object, or you can map the response to an array type. 然后,您可以迭代响应对象的键,或者可以将响应映射到数组类型。

if you add a console log of the responses object and the desired object/array expected then we might be able to help a bit more. 如果你添加了一个响应对象的控制台日志和预期的所需对象/数组,那么我们可能会提供更多帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM