Property 'throw' does not exist on type 'typeof Observable'
Asked Answered
L

2

16

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);        
    }



} 
Lussier answered 2/11, 2018 at 9:47 Comment(4)
what RxJs version?Phobe
sorry iam new to nativscript and angular. i dont know RxJsLussier
RxJs 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).Sartin
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. Is that an error or what is on purpose?Sartin
T
22

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

Thenar answered 10/12, 2018 at 22:14 Comment(0)
T
11

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

Check here

Tipstaff answered 22/6, 2022 at 19:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.