问题描述
我有一个注入HttpService的服务,并以@Interval(20000)开始请求。在间隔函数中,我使用this.http.get(...)向另一台服务器发出请求,但是我看不到任何反应,无论是请求还是异常。我只看到控制台日志“ handleInterval”! 怎么了?
:
import {HttpException,HttpService,Injectable} from '@nestjs/common'
@Injectable()
export class AppService {
constructor(private readonly http: HttpService) {}
@Interval(20000)
handleInterval() {
console.log('handleInterval');
let response = this.http.get('192.168.0.162:8081/diag.fhtml',{responseType: 'arraybuffer'}).pipe(
map(res => {
console.log('received data');
return res.data;
}),catchError(e => {
console.error(e);
throw new HttpException(e.response.data,e.response.status);
}));
}
:
:
}
解决方法
嵌套的HttpService
利用了RxJS
的可观察对象。要触发请求,您需要添加.subscribe()
或使函数async
并添加.toPromise()
。