如何在nestjs Interceptor()中合并2个Observables?

huangapple go评论82阅读模式
英文:

How to merge 2 Observables in nestjs Interceptor()?

问题

我正在开发一个nestjs拦截器,用于通过REST API发送的地址进行地理定位。
以下是代码部分:

export class PointsInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler): Observable<any> {

    var monaddress = context.switchToHttp().getRequest().body
    const geoPremiseToObservable = from(geocoder.geocode(monaddress));
    geoPremiseToObservable.subscribe(value => console.log(JSON.stringify(value)));

    return next.handle().pipe(map(value => console.log(value)))
  }
}

此代码的结果是:

  • 从HTTP响应中获得的JSON:
{
  "address": '123, avenue des Champs Elysées, 75008 PARIS',
  "_id": new ObjectId("64bd089088f925780705bbbd"),
  "__v": 0
}
  • 来自geoCoder的对象:
[{"latitude":48.872438,"longitude":2.29811,"state":"75, Paris, Île-de-France","city":"Paris","zipcode":...}]

我希望将所有这些内容连接在一个JSON中,然后将其“推送到数据库”。
我尝试过使用mergemergeWith,但都没有成功。

任何帮助将不胜感激。

谢谢您。

英文:

I am developping a nestjs interceptor to geolocate an address I am sending throught a rest API.
Here is the code:

export class PointsInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler): Observable&lt;any&gt; {

    var monaddress = context.switchToHttp().getRequest().body
    const geoPremiseToObservable = from(geocoder.geocode(monaddress));
    geoPremiseToObservable.subscribe(value =&gt; console.log(JSON.stringify(value)));

    return next.handle().pipe(map(value =&gt; console.log(value)))
  }

}

The result of this code is:

  • a JSON coming from the http response:
{
  address: &#39;123, avenue des Champs Elys&#233;es, 75008 PARIS&#39;,
  _id: new ObjectId(&quot;64bd089088f925780705bbbd&quot;),
  __v: 0
}
  • An Object coming from geoCoder
[{&quot;latitude&quot;:48.872438,&quot;longitude&quot;:2.29811,&quot;state&quot;:&quot;75, Paris, &#206;le-de-France&quot;,&quot;city&quot;:&quot;Paris&quot;,&quot;zipcode&quot;:...}]

My wish is to have all of this concatenated in one JSON that will be "pushed to the database".
I tried to apply a merge or even a mergeWith. In vain.

Any help would be greatly appreciated.

Thank You.

答案1

得分: 1

export class PointsInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
    const monaddress = context.switchToHttp().getRequest().body;
    const geocodeResult = from(geocoder.geocode(monaddress));
    const geoCoder = await lastValueFrom(geocodeResult);

    return next.handle().pipe(
      tap(value => console.log(value)),
      map(value => {
        //1: Returning an object from both next.handle() and geoPremiseToObservable
        return {
          httpResponse: value,
          geoCoder: geoCoder
        };

       //2: Returning an single object by merging the geocoder response with http response
        return {
          ...value,
          geoCoder: geoCoder
        };
      })
    );
  }
}
英文:

Can you try tap, like this, so instead of subscribing , get using from and then tap both. Apologies if this does not work, it is not tested

export class PointsInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler): Observable&lt;any&gt; {
    const monaddress = context.switchToHttp().getRequest().body;
    const geocodeResult = from(geocoder.geocode(monaddress));
    const geoCoder = await lastValueFrom(geocodeResult);

    return next.handle().pipe(
      tap(value =&gt; console.log(value)),
      map(value =&gt; {
        //1: Returning an object from both next.handle() and geoPremiseToObservable
        return {
          httpResponse: value,
          geoCoder: geoCoder
        };

       //2: Returning an single object by merging the geocoder response with http response
        return {
          ...value,
          geoCoder: geoCoder
        };
      })
    );
  }
}

huangapple
  • 本文由 发表于 2023年7月23日 19:20:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76747969.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定