首页 > 解决方案 > 从 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;
  }
}

也有人可以澄清我的代码中缺少的类型是什么?

标签: typescriptobservablenestjs

解决方案


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语法,这是另一个有效的选择。


推荐阅读