我不明白如何在我的 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
接口?
@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;
}