question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Model.update(...).catch is undefined

See original GitHub issue

Consider the following:

    Subscription.update({
      _id: { $not: subscription._id },
      startDate: { $lt: today },
      endDate: { $gt: newEndDate }
    }, {
      $set: { endDate: newEndDate }
    }).catch(errorHandler);

This complains that Subscription.update(...).catch is undefined.

Whereas calling it after a save() operation on an item works fine:

user.save().catch(errorHandler);

Also note that I am using Bluebird promises:

mongoose.Promise = require('bluebird');

Mongoose version is 4.4.19.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
adamreisnzcommented, May 29, 2016

Well, I think that’s maybe more of a personal opinion 😃

I don’t find it trivial at all, and there are many use cases for me to only use .catch and not have a .then, for example when doing some background updating of data which doesn’t need to send back a response, but does need error handling in case it goes wrong.

I find it much cleaner to write .catch(doSomething) than something like .then(null, doSomething), as it makes it easier to distinguish and separate success handlers and failure handlers and makes the code easier to read. I find then...catch pretty much analogous to a try...catch.

Also, from my usage experience it has been around for quite a while now and most promise libraries implement the method, as well as .finally(), which in my opinion is also a must have to avoid code duplication in certain circumstances.

If you’d want me to make a PR for this let me know, I’ll be happy to look into it.

2reactions
adamreisnzcommented, Jun 1, 2016

Thanks, I’ll try to look into it this week 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

store for model is undefined - javascript - Stack Overflow
The server returns the data (I do see this using fiddler) but I always get the error Unable to set property 'store' of...
Read more >
Async/Await Error Handling - Beginner JavaScript - Wes Bos
We will talk about error handling strategies for async await in this lesson. Because there is no .then() that we are chaining on...
Read more >
Handling those unhandled promise rejections with JS async ...
One of your await ed functions fails (i.e. rejects a promise); You get the error. Another possibility is that you know you need...
Read more >
TypeError: Cannot read property 'listeners' of undefined #7342
I am getting an error ''TypeError: Cannot read property 'listeners' of undefined" while updating an object through custom method defin...
Read more >
throw - JavaScript - MDN Web Docs - Mozilla
The throw statement throws a user-defined exception. Execution of the current function will stop (the statements after throw won't be ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found