0
votes

I need to get all my alarms form web service in Nativescript. But .map doesn't function. I tried this function. In Angular 5 works good, but in Nativescript show this error:

[ts] Property 'map' does not exist on type 'Observable'. in this .map((response: Response) => {

I import this: import 'rxjs/add/operator/map'; import { Http, Headers, Response } from '@angular/http'; but nothing happens

My "rxjs": "~6.1.0",

 public AlarmsGetAll(): Observable<Alarm[]> {
    let headers = new Headers();
    headers.append('x-access-token', 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaWQiOnsiVXNlcklEIjoiMzEwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAifSwiaWF0IjoxNTI4ODgzNDU1LCJleHAiOjE1Mjk3NDc0NTV9.qei48rzt1KF9AKIEz8Aq-A8pWkESc1G3jBUfdU27oYE');
    return this.http.get(Api.getUrl(Api.URLS.AlarmsGetAll), {
      headers: headers
    })
      .map((response: Response) => {
        let res = response.json();
         console.log(res)
        if (res.StatusCode === 1) {
        } else {
          return res.StatusDescription.map(alarm => {
            //console.log(alarm)
            return new Alarm(alarm);
          });
        }
      });
  }

Can you ask me any idea, what is the problem?

3
@martin I wonder what import mechanism will be in v7 😉Royi Namir

3 Answers

2
votes

If you use rxjs@6, you should update operator.

Rxjs 5

import 'rxjs/add/operator/map'

myObservable
  .map(yourdata=> yourdata + 15)
  .subscribe(...);

Rxjs 6

import { map } from 'rxjs/operators';

myObservable
  .pipe(map(yourdata=> yourdata + 15))
  .subscribe(...);

Solution in your case :

So you need to update your import and use the new way of Rxjs6

import { map } from 'rxjs/operators';

 public AlarmsGetAll(): Observable<Alarm[]> {
    let headers = new Headers();
    headers.append('x-access-token', 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaWQiOnsiVXNlcklEIjoiMzEwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAifSwiaWF0IjoxNTI4ODgzNDU1LCJleHAiOjE1Mjk3NDc0NTV9.qei48rzt1KF9AKIEz8Aq-A8pWkESc1G3jBUfdU27oYE');
    return this.http.get(Api.getUrl(Api.URLS.AlarmsGetAll), {
      headers: headers
    })
      .pipe(map((response: Response) => {
        let res = response.json();
         console.log(res)
        if (res.StatusCode === 1) {
        } else {
          return res.StatusDescription.map(alarm => {
            //console.log(alarm)
            return new Alarm(alarm);
          });
        }
      }));
  }

If you need more information

1
votes

Here is your code modified with the latest version of rxjs v6 and HttpClientModule (HttpModule is deprecated since v5):

import { HttpClient, HttpHeaders, HttpResponse } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

public AlarmsGetAll(): Observable<Alarm[]> {
let headers = new HttpHeaders();
headers.append('x-access-token', 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaWQiOnsiVXNlcklEIjoiMzEwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAifSwiaWF0IjoxNTI4ODgzNDU1LCJleHAiOjE1Mjk3NDc0NTV9.qei48rzt1KF9AKIEz8Aq-A8pWkESc1G3jBUfdU27oYE');
return this.http.get(Api.getUrl(Api.URLS.AlarmsGetAll), {
  headers: headers,
  observe: 'response'
}).pipe(
  map((response: HttpResponse<any>) => {
    let res = response.json();
     console.log(res)
    if (res.StatusCode === 1) {
    } else {
      return res.StatusDescription.map(alarm => {
        //console.log(alarm)
        return new Alarm(alarm);
      });
    }
  }));
}

As mentionned previously you can check the migration guideline to update rxjs v5 to v6 and here is the documentation for HttpClientModule : Angular http documentation

0
votes

The latest NativeScript uses Angular 6 and Rxjs 6.1.0 and above. Check the migration guidelines for moving from rxjs@5 to rxjs@6