peryztor
peryztor

Reputation: 175

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

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?

Upvotes: 5

Views: 9489

Answers (5)

EdgarMorales
EdgarMorales

Reputation: 29

In Angular 6x with rxjs 6.3.3 you can do this. In the file(app.component.ts)

import { Component } from '@angular/core';
import { HttpClient, HttpParams, HttpHeaders} from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { map, catchError, retry } from 'rxjs/operators';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})

export class AppComponent 
{    
  _url = 'http://...';
  constructor( private http: HttpClient ) { }
  articles: Observable<any>;

  // Method and constructor
  getAPIRest() 
  {       
    const params = new HttpParams().set('parameter', 'value');
    const headers = new HttpHeaders().set('Autorization', 'auth-token');
    this.articles = this.http.get(this._url + '/articles', { params, headers })
                 .pipe( retry(3),
                        map((data => data),
                        catchError(err => throwError(err))));
  }
}

Upvotes: 1

Viclotana
Viclotana

Reputation: 61

you have to import map in ng6 like so:

import { map } from 'rxjs/operators';

Upvotes: 1

Laurie Clark
Laurie Clark

Reputation: 630

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

Upvotes: 3

Vignesh
Vignesh

Reputation: 2384

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(...);

Upvotes: 5

Citizen
Citizen

Reputation: 11

try this:

npm install rxjs@6 rxjs-compat@6 --saven

please visit Angular 2 beta.17: Property 'map' does not exist on type 'Observable<Response>' for more explnation.

Upvotes: 0

Related Questions