从 HttpModule 中可观察的 RxJS 中检索数据



我不明白如何在我的 NestJS 应用程序中map HttpService数据属性。据我了解,这个Observable只是包裹axios.下面是一些示例代码:

interface Todo {
   task: string,
   completed: false
}
import {
  Injectable,
  HttpService,
  Logger,
  NotFoundException,
} from '@nestjs/common'
import { map } from 'rxjs/operators
async getTodo(todoUrl: string): Todo {
   const resp = this.httpService
      .get('https://example.com/todo_json')
      .pipe(map(response => response.data)) // map task/completed properties?
   return resp
}

在这种情况下resp似乎是 Observable 型。如何仅检索我想要在此请求上使用map的数据属性以返回我的Todo接口?

默认情况下,Nest 将为您订阅可观察量,然后您从服务中返回可观察量。在这种情况下,您可以执行以下操作

@Injectable()
export class TodoService {
  constructor(private readonly http: HttpService) {}
  getTodos(todoUrl: string): Observable<Todo> {
    return this.http.get(todoUrl).pipe(
      map(resp => resp.data),
    );
  }
}

只要您有一个控制器类调用this.todoSerivce.getTodos(todoUrl)并返回它,响应就会被发送出去。

但是,如果你想把它作为一个承诺,因为你更习惯于它们,你可以对可观察链附加一个.toPromise()方法,现在它可以等待(尽管它会更慢,因为它必须等待可观察量发出它的完整事件(。

带有.toPromise()的示例:

@Injectable()
export class TodoService {
  constructor(private readonly http: HttpService) {}
  getTodos(todoUrl: string): Todo {
    const myTodo = await this.http.get(todoUrl).pipe(
      map(resp => resp.data),
    ).toPromise();
    return myTodo;
  }
}
<小时 />

编辑 22-1-20

RxJS@^7中,toPromise()被弃用,并将在v8中删除。相反,您可以使用lastValueFrom来包装整个可观察量

@Injectable()
export class TodoService {
  constructor(private readonly http: HttpService) {}
  getTodos(todoUrl: string): Todo {
    const myTodo = await lastValueFrom(this.http.get(todoUrl).pipe(
      map(resp => resp.data),
    ));
    return myTodo;
  }
}

查看您的代码:

import { map } from 'rxjs/operators' 
async getTodo(todoUrl: string): Todo {
   const resp = this.httpService
      .get('https://example.com/todo_json')
      .pipe(map(response => response.data)) // map task/completed properties?
   return resp
}

getTodo返回可观察量,而不是响应。所以你的返回值应该是Observable<Todo>

代码应该更像这样:

  getTodo(): Observable<Todo> {
    return this.http.get<Todo>('https://example.com/todo_json')
      .pipe(
        map(response => response.data),
        catchError(this.handleError)
      );
  }

编辑:你不能只从此方法返回数据,因为它是异步的。它还没有数据。该方法返回一个可观察的...这基本上是一个合同,说它将(稍后(为您返回数据。

异步函数需要返回一个承诺,可以在一个可观察量上调用 toPromise 来返回一个承诺。

async getTodo(todoUrl: string): Todo {
   const resp = this.httpService
      .get('https://example.com/todo_json')
      .pipe(map(response => response.data)) // map task/completed properties?
   return resp.toPromise();
}
async getTodo(todoUrl: string): Todo {
    const resp = await this.httpService
      .get('https://example.com/todo_json')
      .toPromise();
    return resp.data;
}

最新更新