I have created a Class implementing NestInterceptor().
As it returns an Observable(), when I have a promise in it, it does not wait the promise to return the data. How should I do that? Here is my code:
export class PointsInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
var monaddress = context.switchToHttp().getRequest().body
var retour
//Using Promise
geocoder.geocode(monaddress)
.then(function(res) {
console.log('res: '+ res);
retour = res[0].latitude;
})
.catch(function(err) {
console.log(err);
retour = err
}) ;
return next.handle().pipe(map(retour => ({ retour })))
}
}
return is executed before the geocoder promise. I tried to put the return on success and error. A warning is rised:
A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value.ts(2355)
PS: As this is a nestJS interceptor, I have to keep the class returning the Observable().
Why don't you use async/await
syntax like below
async intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
var monaddress = context.switchToHttp().getRequest().body
var retour
cost res = await geocoder.geocode(monaddress)
console.log('res: '+ res);
retour = res[0].latitude;
return next.handle().pipe(map(retour => ({ retour })));
}