I am creating an observable (RxJS) in NodeJS using ESM. Inside the observable I am performing a query with mongoose. I get the result, but the console returns errors (detailed at the end of the question).
I have tried to perform the operation with Javascript promises and without promises trying to handle the errors with the operators (try catch).
//-----------------------------------------------------------------//
// WITHOUT PROMISES:
//-----------------------------------------------------------------//
// Create observable obsAuth:
const obsAuth = new Observable(async (observer) => {
try{
const doc = await people.Model.findOne();
observer.next(doc);
} catch (err) {
observer.error(err);
}
// Finish observer:
observer.complete();
});
// Observe sub-element content (Subscribe):
let subAuth = obsAuth.subscribe({
next: doc => {
res.status(200).send({ success: true, people_data: doc });
console.log(doc);
},
error: err => {
res.status(500).send({ success: false, error: err });
console.error('Error: ' + err);
},
complete: () => console.log('Suscripción finalizada')
});
// Unsuscribe:
subAuth.unsubscribe();
//-----------------------------------------------------------------//
//-----------------------------------------------------------------//
// WITH PROMISES:
//-----------------------------------------------------------------//
// Build people query:
const peopleQueryMongoDB = people.Model.findOne();
// Create observable obsAuth:
const obsAuth = new Observable(async (observer) => {
// Excecute people query (Promise):
await peopleQueryMongoDB.exec()
.then((doc) => {
observer.next(doc);
})
.catch((err) => {
observer.error(err);
});
// Finish observer:
observer.complete();
});
// Observe sub-element content (Subscribe):
let subAuth = obsAuth.subscribe({
next: doc => {
res.status(200).send({ success: true, people_data: doc });
console.log(doc);
},
error: err => {
res.status(500).send({ success: false, error: err });
console.error('Error: ' + err);
},
complete: () => console.log('Suscripción finalizada')
});
// Unsuscribe:
subAuth.unsubscribe();
//-----------------------------------------------------------------//
In both cases I have exactly the same behavior. I get the results and with the following error in the console.
CONSOLE ERRORS:
node:internal/process/promises:246
triggerUncaughtException(err, true /* fromPromise */);
^
Error
...
...
at processTicksAndRejections (node:internal/process/task_queues:96:5) {
message: '1 errors occurred during unsubscription:\n' +
'1) TypeError: finalizer.unsubscribe is not a function',
errors: [
TypeError: finalizer.unsubscribe is not a function
...
...
at processTicksAndRejections (node:internal/process/task_queues:96:5)
]
}
Thanks a lot in advance!!