将查询结果存储在局部变量中并在 Angular 中发送相同的确认

Store result of query in local variable and send a confirmation for the same in Angular

我的节点服务器正在给我一个查询结果的响应,我想将其存储在我的 Angular 服务的局部变量(电影)中并传递一条确认消息({"result":true} ) 到询问 angular 组件。

redirect(id){
    this.movie.getMovie(id).subscribe( confirmation =>{ 
      if(confirmation.result) this.router.navigate(["/movies/movDetails"]);
    });
}

这是我的angular组件

getMovie(id):Observable<any>{
    return this.http.post("http://localhost:3000/getMovie",{ "_id":id }).subscribe(IncomingValue => this.movie = IncomingValue).pipe(map( return {"result":true} ));
} 

服务组件

检索数据时,人们经常使用 get,而不是 post。这就是我的一个简单 get 的样子:

  getProducts(): Observable<IProduct[]> {
    return this.http.get<IProduct[]>(this.productUrl);
  }

使用您的代码...然后您可以使用 RxJS 管道运算符来执行其他操作:

getMovie(id):Observable<any>{
    return this.http.post("http://localhost:3000/getMovie",{ "_id":id })
           .pipe(
              tap(IncomingValue => this.movie = IncomingValue),
              map(() => {return {"result":true}} )
           );
} 

第一个管道运算符 tap 将传入值存储在本地 属性。

第二个管道运算符 map 将结果映射为定义的键值对。

希望这对您有所帮助。