Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type 'object' must have a 'Symbol.iterator' method that returns an iterator

data.technologies is an array and I would like to get the output of that array.

This is how the output of data.technologies looks like:

enter image description here

this.profileService.getEntities().subscribe(data => {
  for (const technology of data.technologies) {
    console.log(technology);
  }
});

The type of getEntities() is Observable<IprofileData>.

export interface IprofileData {  
  technologies: object;
}

TypeScript gives me the error:

TS2488: Type 'object' must have a 'Symbol.iterator' method that returns an iterator.

What am I doing wrong?

like image 348
xRay Avatar asked Jan 09 '20 15:01

xRay


3 Answers

Is data.technologies a function (you mentioned it "returned" an array)? If so, you simply need to call it like this:

this.profileService.getEntities().subscribe(data => {
    for (const technology of data.technologies()) {
        console.log(technology);
    }
});
like image 20
emeraldsanto Avatar answered Nov 13 '22 05:11

emeraldsanto


For future googlers:

This is also the error message returned if you forget to objectify your function inputs.

Example:

setCurrRowData(...currRowData, sort_order: num);

Fix:

setCurrRowData( { ...currRowData, sort_order: num } );
//              ^                                 ^
like image 121
cssyphus Avatar answered Nov 13 '22 04:11

cssyphus


Runtime structure is correct, it is an array, but the type says (from your comment) that it is just an object. Object has no iterator protocol, array has.

Example type which should fulfill the need:

type Data {
  ...// some other props
  technologies: string[] // change string into any type you have there
}
like image 28
Maciej Sikora Avatar answered Nov 13 '22 05:11

Maciej Sikora