Hello,
I’m going through a tutorial and I’m bit stuck code that I’m not able to figure out and hoping someone can let me know what is this I’m doing wrong.
Not sure if it matters but the tutorial is using POST and due to configuration I’m using GET to work through the tutorial.
The message I’m getting is:EXCEPTION: Cannot read property ‘0’ of undefined, which I’m assuming, I’m not hitting the right value from the returned array. correct me if im wrong.
problem area is the second map function. without it the script works but I like to follow the tutorial and see what it is that Im doing wrong. how do i set the two values: this.loggedIn and localstorage?
userAuthenticationMethod(uInfo: any): Observable<any[]>{
let params: URLSearchParams = new URLSearchParams();
params.set('userName', uInfo.userName);
params.set('passWord', uInfo.password);
return this._http
.get(this.userUrl, {search: params})
.map((res: Response) => res.json())
.map((res) => {
if (res.userName) {
// localStorage.setItem('auth_token', res.userName);
this.loggedIn = true;
}
return res.userName;
});
//.catch(this.handleError);
}//END - userAuthenticationMethod
the snippet of code from the tutorial
login(email, password) {
let headers = new Headers();
headers.append('Content-Type', 'application/json');
return this.http
.post(
'/login',
JSON.stringify({ email, password }),
{ headers }
)
.map(res => res.json())
.map((res) => {
if (res.success) {
localStorage.setItem('auth_token', res.auth_token);
this.loggedIn = true;
}
return res.success;
});
}