Angular 类型“Promise上不存在属性“token”
我想处理一个令牌。但要明白这一点: 类型“Promise”上不存在属性“token” 我有以下代码。我得到一个带有属性标记的json。顺便说一句,我的指导是使用Angular 2的教程。我已经试过了,没有['token']。但是没有成功。我还为帖子提供了一个界面,但我没有使用它来消除错误。它也不适用于接口 这是我的密码:Angular 类型“Promise上不存在属性“token”,angular,token,Angular,Token,我想处理一个令牌。但要明白这一点: 类型“Promise”上不存在属性“token” 我有以下代码。我得到一个带有属性标记的json。顺便说一句,我的指导是使用Angular 2的教程。我已经试过了,没有['token']。但是没有成功。我还为帖子提供了一个界面,但我没有使用它来消除错误。它也不适用于接口 这是我的密码: signin(email: string, password: string) { return this.http.post<any>('http://1
signin(email: string, password: string) {
return this.http.post<any>('http://127.0.0.1:8000/api/userLogin',
{ email: email, password: password },
this.httpOptions).map((response: Response) => {
const token = response.json()['token'];
const base64Url = token.split('.')[1];
const base64 = base64Url.replace('-', '+').replace('_', '/');
return JSON.parse(window.atob(base64));
});
}
为什么不使用Serializable类呢 这是我针对这个问题的解决方案,参考: 当您调用登录函数时
signIn(user: User) { const body = JSON.stringify(user);
headers: new HttpHeaders({"Content-Type": "application/json"})
return this.http.post("http://localhost:3000/users/login", body, httpOption)
.pipe(map((response) => {
let test = new AIPResponse().fromJSON(response);
localStorage.setItem("token", test.token);
localStorage.setItem("userId", test.message);
}))
.pipe(catchError((error) => throwError(error)));
}
希望能对你有所帮助 它应该是Response.token您的API响应是什么?您在哪里调用和订阅了signin?您是否尝试过response.json.token@海格,回答中包括成功吗?API的响应如下->成功:{token:eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6IjllOW…lsJiIWVmiiNY1Ft02MSWGS-Thx7_warYrUucP8bPHHnyMCfnU}?您在哪里订阅了它?
export class MdfResponse {
public message?: string;
public token?: string;
constructor() {}
fromJSON(json) {
for (var propName in json)
this[propName] = json[propName];
return this;
}
}
signIn(user: User) { const body = JSON.stringify(user);
headers: new HttpHeaders({"Content-Type": "application/json"})
return this.http.post("http://localhost:3000/users/login", body, httpOption)
.pipe(map((response) => {
let test = new AIPResponse().fromJSON(response);
localStorage.setItem("token", test.token);
localStorage.setItem("userId", test.message);
}))
.pipe(catchError((error) => throwError(error)));
}