我向数据库发布一篇文章,我必须制作一个映射来处理该查询的响应。如果在响应中没有我需要的数据之一,则必须进行另一个查询。我使用await/async进行此查询,但在调试时似乎无法正常工作。
代码如下:
// Service (I have changed the endpoints)
public login(username: string, password: string) {
let loginDto = { info: new LoginDto(username, password) };
return this.http.post('https://jsonplaceholder.typicode.com/posts', { title: 'Angular POST Request Example' }).pipe(
map(response => {
return this.processLoginResponse(response);
})
);
}
private async processLoginResponse(response) {
let permissions = await this.getPermissionsFromAPI(116677);
this._user = {username: 'Jhon', permissions: permissions};
return this._user
}
private getPermissionsFromAPI(userId): Promise<any> {
return this.http.get('https://jsonplaceholder.typicode.com/todos/1').toPromise();
}
// Component
onSubmit(e) {
const { username, password } = this.formData;
this.authService
.login(username, password)
.subscribe(
data => {
data.then(x => console.log(x));
// things
}
);
}
switchMap
而不是map
,这样可以在收到第一个HTTP请求(https://jsonplaceholder.typicode.com/posts
)后再发起一个HTTP调用(getPermissionsFromAPI
)。 - Random