Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
Ask Question
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) => {
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!!
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())
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.