Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 6 Property 'map' does not exist on type 'Object'

Tags:

angular6

I have a api which return objects / array like this:

(2) [{...}, {...}]      object

  0: {a: '1', b: {id: '1'}}
  1: {a: '2', b: {id: '2'}}

So it looks like array of objects (but debuges says 'Object').

So in my code I have:

return this.http.get(this.url).pipe(
  map(datas => {
    return datas.map(data => {
      let object = {
        a: data['a'],
        b: data['b']['id'],
      }
      return object;
    })
  })
);

but there:

return datas.map(data => {

I got an error:

Property 'map' does not exist on type 'Object'.

But application is working well is correctly shows this data. But this error is annoying.

What can I do?

like image 746
peryztor Avatar asked May 25 '18 11:05

peryztor


People also ask

Does not exist on type object?

The "Property does not exist on type Object" error occurs when we try to access a property that is not contained in the object's type. To solve the error, type the object properties explicitly or use a type with variable key names.

What is map function in TypeScript?

map() is an inbuilt TypeScript function that is used to create a new array with the results of calling a provided function on every element in this array.

Does not exist on type never []'?

The error "Property does not exist on type 'never'" occurs when we try to access a property on a value of type never or when TypeScript gets confused when analyzing our code. To solve the error, use square brackets to access the property, e.g. employee['salary'] . Here is an example of how the error occurs.


2 Answers

The following operators were renamed in RXJS6

catch() => catchError()
do() => tap()
finally() => finalize()
switch() => switchAll()

Additionally, some Observable-creation methods were renamed/ refactored:

throw() => throwError()
fromPromise() => from() (this automatically detects the type)

FOR MAP syntax

import { map } from 'rxjs/operators';

myObservable
  .pipe(map(data => data * 2))
  .subscribe(...);
like image 193
Vignesh Avatar answered Nov 15 '22 07:11

Vignesh


I had to specify the type of the return value with (data: any) => { ... }

like image 43
Laurie Clark Avatar answered Nov 15 '22 09:11

Laurie Clark