英文:
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中,然后将其“推送到数据库”。
我尝试过使用merge或mergeWith,但都没有成功。
任何帮助将不胜感激。
谢谢您。
英文:
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<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)))
}
}
The result of this code is:
- a JSON coming from the http response:
{
address: '123, avenue des Champs Elysées, 75008 PARIS',
_id: new ObjectId("64bd089088f925780705bbbd"),
__v: 0
}
- An Object coming from geoCoder
[{"latitude":48.872438,"longitude":2.29811,"state":"75, Paris, Île-de-France","city":"Paris","zipcode":...}]
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<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
};
})
);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论