typescript - 从 Nestjs 中的 observable 中的响应返回数据
问题描述
我是Nestjs、 Typescript 和基本上后端开发的新手。我正在开发一个简单的 Weather 应用程序,我从Open Weather API获取天气数据。我使用的HttpModule
是内置的 Nest,它将 Axios 包装在其中,然后HttpService
用于发出 GET 请求以打开天气。该请求返回一个 Observable,这对我来说完全是新闻。如何从 中的 observable 中提取实际响应数据Injectable service
并将其返回给Controller
?
这是我的 weather.service.ts
import { Injectable, HttpService } from '@nestjs/common';
@Injectable()
export class AppService {
constructor(private httpService: HttpService) {}
getWeather() {
let obs = this.httpService.get('https://api.openweathermap.org/data/2.5/weather?q=cairo&appid=c9661625b3eb09eed099288fbfad560a');
console.log('just before subscribe');
obs.subscribe((x) => {
let {weather} = x.data;
console.log(weather);
})
console.log('After subscribe');
// TODO: Should extract and return response data f
// return;
}
}
这是weather.controller.ts
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';
@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}
@Get()
getWeather() {
const res = this.appService.getWeather();
return res;
}
}
也有人可以澄清我的代码中缺少的类型是什么?
解决方案
RxJS Observables
本质上是高级回调。因为它们以异步方式工作,所以您需要让您的代码处理它。Nest 可以处理从控制器返回的 Observable 并在后台为您订阅它,因此您在服务中需要做的就是这样:
import { Injectable, HttpService } from '@nestjs/common';
@Injectable()
export class AppService {
constructor(private httpService: HttpService) {}
getWeather() {
return this.httpService.get('https://api.openweathermap.org/data/2.5/weather?q=cairo&appid=c9661625b3eb09eed099288fbfad560a').pipe(
map(response => response.data)
);
}
}
map
是从其中导入的,rxjs/operators
并且与它类似Array.prototype.map
,因为它可以将值输入并根据需要对其进行转换。从这里开始,您Controller
只需要返回this.appService.getWeather()
,其余的由 Nest 处理。
您拥有的另一个选择是将 observable 转换为 Promise using.toPromise()
然后您可以使用通常的async/await
语法,这是另一个有效的选择。
推荐阅读
- c# - 错误 1053:服务未使用 FileSystemWatcher 及时响应启动或控制请求
- python - 根据多个匹配列生成值列表
- android - 如何在安装和打开应用程序后立即设置3个连续的权限请求?
- javascript - 如何使粘贴的 HTML 在编辑器中被识别为 Blot?
- shell - 尝试使用 QCTools_V2 时如何解决“错误 (genfile::MalformedInputError)”的问题
- r - 在数据表中汇总多重回归的结果
- reactjs - react 无法编译自己的 JSX 语法
- python - Django - 调用 values_list 时出错:未定义“平面”
- jenkins - 无法从詹金斯的共享库中运行 groovy 的 GPARS
- java - RabbitMq 中的反序列化