After upgrading to NestJS v8, I had to upgrade my RxJS version as well from 6 to 7 then it started throwing ERROR [ExceptionsHandler] no elements in sequence
error.
This is a sample method in one of the app services:
show(): Observable<any> {
return from(this.repository.fetch()).pipe(
filter((data) => data.length > 0),
map((data) => data.map((datum) => parseData(datum)),
);
}
While I had NestJS v7 and RxJS v6, the method was working just fine; in other words, if the filter
operation is not passed, the map
operator wouldn't be called at all and the Observable stops there.
But after upgrading to NestJS v8 and RxJS v7, if my repository does not return any data, the app starts throwing ERROR [ExceptionsHandler] no elements in sequence
error.
A workaround I came up with is as follows:
show(): Observable<any> {
return from(this.repository.fetch()).pipe(
filter((data) => data.length > 0),
defaultIfEmpty([]),
map((data) => data.map((datum) => parseData(datum)),
);
}
This way the error is gone but I have two more problems:
1- the map
operator still runs which I do not want
2- the second one which is way more important to me is that I have to update all my services/methods which have a validation like this which is really crazy.
my dependencies are as follows:
"dependencies": {
"@nestjs/common": "^8.4.2",
"@nestjs/core": "^8.4.2",
"rxjs": "^7.5.5"
},
first()
operator, if the subscription is completed without having emitted a value. Are you usingfirst
anywhere? – ByerslastValueFrom
under the hood. If there's no value emitted then there will be a problem. Try moving thedefaultIfEmpty
to below themap
operator – NorvinlastValueFrom
? One more question: Isn't there a way without addingdefaultIfEmpty
because this way I have to update the whole source code to stop getting 500 errors. Any workaround? – TransformtoPromise
which is why we changed tolastValueFrom
. Instead of adding this to every service you could add it to aninterceptor
and have the interceptor set thedefaultIfEmpty
for you – Norvin