Issue
import { Component, OnInit } from '@angular/core';
import { AuthService } from '../auth.service';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
LoginUserData= { email: '', password:'' };
constructor( private _auth : AuthService ) { }
ngOnInit(): void {
}
luser()
{
this._auth.loginUser(this.LoginUserData)
.subscribe( Response =>{
console.log(Response);
localStorage.setItem('token',Response.token)
})
}
}
I am getting error in localStorage.setItem('token',Response.token)
, and when I'm trying to store token in register page, it has been stored in localStorage
Error: Property 'token' does not exist on type 'Object'.ts(2339)
This is the error, and this is the GitHub file.
Solution
There's no definition of type Response
in your code. Response
is also a reserved word I think. For your own good sake, stop naming variable with capital letters.
Try this to save the token in LS.
this._auth.loginUser(this.LoginUserData)
.subscribe((response: any) => {
console.log(response);
localStorage.setItem('token', response.token);
})
I assume that your token is string
. If it's not, you will need to do localStorage.setItem('token', JSON.stringify(response.token));
Answered By - Nehal
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.