Subscription does not exist of type "Subscription"

I am new to angualr 2. And I am making a small application for practice. I have a service to authenticate users when they login. And here is the login function.

login(email:string, password:string){

        var loginrul="https://filing.herokuapp.com/api/v1/auth/login/";
        return this.http.post(loginrul,JSON.stringify({  password: password,email: email }),
            {headers:new Headers({'Content-Type':'application/json'})}
            ).map(res=>res.json()).
            subscribe(
                data => localStorage.setItem('id_token',data.auth_token),
                error=>console.log(error)
            );
    }

      

When I call this function in the angualr component, it throws the error "Subscription signature does not exist in type Subscription."

doLogin(event) {
        console.log(event);
        console.log(this.loginForm.value);


        let formData = this.loginForm.value;
        let email = this.loginForm.controls.email.value;
        let password= this.loginForm.controls.password.value;
        this.auth.login(email,password).subscribe(res =>{
            console.log(res);
            if(res.status===200){
                this.router.navigate(['dashboard']);
            }
        }
        )
    }

      

I think why angular is giving this error is because I am using subscription in the login function as well as when calling the login function. Please suggest some solution or alternative approach to achieve the result I am trying to achieve in the login function.

+3


source to share


1 answer


Your own analysis of the problem is correct! Remove .subscribe

in your service and only use it in your component where you use it. Your service should return after the operator .map

.



If you subscribed to the service, it will return Subscription

instead Observable

, which is why you are getting your error.

+6


source







All Articles