11

Iam learning nativescript+angular for developing android and ios apps.Iam working and learning basic services of nativescript+angular.In the post method of the my project i have the error 'property 'throw' does not exist on type 'typeof Observable'. My Code is:

import { User } from "./user";
import { Config } from "../config";
import { Injectable } from "@angular/core";
import { Observable } from "tns-core-modules/ui/page/page";

@Injectable()
export class UserService {
    constructor(private http: Http) { }

    register(user: User) {
        let headers = new Headers();
        headers.append("Content-Type", "application/json");

        return this.http.post(
            Config.apiUrl + "Users",
            JSON.stringify({
                Username: user.email,
                Email: user.email,
                Password: user.password
            }),
            { headers: headers }
        )
            .catch(this.handleErrors);

    }

    handleErrors(error:Response)
    {
        console.log(JSON.stringify(error.json()));
        return Observable.throw(error);        
    }



} 
Arigarasuthan
  • 315
  • 1
  • 8
  • 17
  • what RxJs version? – Andrei Tătar Nov 02 '18 at 09:58
  • sorry iam new to nativscript and angular. i dont know RxJs – Arigarasuthan Nov 02 '18 at 10:02
  • [RxJs](http://reactivex.io/rxjs/manual/overview.html#observable) is the library that provides observables that are used in Angular. RxJs version is important because `Obserble.throw` is now deprecated in recent versions of RxJs, and people should use `throwError` instead (see @slejnej answer below). – Nino Filiu Dec 10 '18 at 22:24
  • I also observe that you wrote `import { Observable } from "tns-core-modules/ui/page/page";` instead of the `import { Observable } from 'rxjs';` [used in web dev](https://angular.io/guide/rx-library). Is that an error or what is on purpose? – Nino Filiu Dec 10 '18 at 22:29

2 Answers2

20

Observable.throw is now deprecated. You have to use this instead:

import { throwError } from 'rxjs';

then replace your Observable.throw with throwError("Your error"). Your subscriber to observable will picked it up in the same way it did in the past.

Check here @ #287

finefoot
  • 9,914
  • 7
  • 59
  • 102
slejnej
  • 310
  • 1
  • 6
5

Observable.throw and throwError("Your error") are now deprecated. Now, you should use a factory directly as throwError(() => new Error('Your error'))

Check here

Adam Perea
  • 320
  • 2
  • 9