'NodeJS with ESM: internal/process/promises:246 | triggerUncaughtException(err, true /* fromPromise */) | finalizer.unsubscribe is not a function
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!!
Solution 1:[1]
Experienced the same issue yesterday.
The problem with your code is that you are giving the Observable a promise as Teardown logic.
The easiest way to restructure your code so it works would be
const obsAuth = new Observable((observer) => {
(async () => {
// Excecute people query (Promise):
await peopleQueryMongoDB.exec()
.then((doc) => {
observer.next(doc);
})
.catch((err) => {
observer.error(err);
});
})().then(ignored => observer.complete()) // Finish observer:
});
but you can also just use
fromPromise(peopleQueryMongoDB.exec())
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |