【发布时间】:2019-06-22 13:12:52
【问题描述】:
在我的nest.js 项目中,我向一个开放的API 发出一个get 请求。如果 API 没有结果,我会得到 HTTP 状态 400。在这种情况下,我想从 Observable 返回一个空的静态对象。
this.getData(data.title)
.subscribe((resp) => {
console.log(resp);
});
可观察
getData(title: string){
const AuthStr = 'Bearer '.concat(API.TOKEN);
const requestOptions = {
headers: { Authorization: AuthStr }
};
const emptyResponse = {
"data": [{"id": ''}]
}
try {
return this.httpService.get(API.ID_URL + title, requestOptions);
} catch (error) {
if(error.response.status == 400){
return of(JSON.stringify(emptyResponse)); //this doesn't work
}
this.errorHandling(error);
}
}
【问题讨论】:
标签: node.js typescript observable axios nestjs