我得到异常'无法读取属性'订阅'未定义'但我不知道我做错了什么。
checkUserCredentials() {
let response;
let user = {email: this.email, password: this.password};
this.userService.getUser(user).subscribe((res) => response = res);
}
服务:
getUser(user): any {
//this.http.get('http://localhost:3000/users?email = '+user.email+'&password='+user.password,
let headers = new Headers();
let postResponse;
headers.append('Content-Type', 'application/json');
this.http.post('http://localhost:3000/users',
JSON.stringify(user),
{headers:headers})
.map((res: Response) => res.json())
}
答案 0 :(得分:4)
您需要在getUser
方法中返回:
getUser(user): any {
let headers = new Headers();
let postResponse;
headers.append('Content-Type', 'application/json');
return this.http.post('http://localhost:3000/users',
JSON.stringify(user),
{headers:headers})
.map((res: Response) => res.json())
}
答案 1 :(得分:2)
缺少return
。该方法不返回任何内容,因此隐式返回undefined
,这会导致错误:
getUser(user): any {
//this.http.get('http://localhost:3000/users?email = '+user.email+'&password='+user.password,
let headers = new Headers();
let postResponse;
headers.append('Content-Type', 'application/json');
// vvv missing return
return this.http.post('http://localhost:3000/users',
JSON.stringify(user),
{headers:headers})
.map((res: Response) => res.json())
}