Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property 'throw' does not exist on type 'typeof Observable'

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



} 
like image 869
Arigarasuthan Avatar asked Dec 11 '22 04:12

Arigarasuthan


2 Answers

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

like image 115
slejnej Avatar answered Mar 20 '23 13:03

slejnej


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

Check here

like image 28
Adam Perea Avatar answered Mar 20 '23 12:03

Adam Perea