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.

Throw clean error if save() called in parallel on same doc instance

See original GitHub issue

I have seen a few issues closed already regarding the #save() method and swallowing of exceptions. I’ve got this happening at the moment with the latest version in npm (4.4.12). Here’s some code:

const giftcard = new Giftcard({ // new instance of the model
  // setting some properties here
});

giftcard.save((err) => {
  if (err) throw new Error(err);
  // this never happens - the callback is never called
});

I can post a more detailed example if required.

Edit: Also, this happens with the promise interface as well, so giftcard.save().then() has the same result.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:8
  • Comments:61 (13 by maintainers)

github_iconTop GitHub Comments

7reactions
niftylettucecommented, Jul 1, 2016

@mtimofiiv @cwhenderson20 @dodekeract I have just donated over PayPal to @vkarpov15 (val@karpov.io) for his time helping me debug this issue. If you have been able to get passed this through our findings please send him some dough! 💸 He deserves it! He spent a ton of time with me, even while he was on his mobile phone he was helping!

6reactions
cwhenderson20commented, Jun 10, 2016

@vkarpov15 I am experiencing this same issue on version 4.4.20. Attempting to call save on my model results in the callback never being called while running the same operation with $__save works just fine.

I am not overwriting promises and I do not have any pre-save (or otherwise) middleware for the model and using mongoose.set("debug", true) does not show anything strange. I have also verified that the document I’m attempting to save does pass validation.

I have additionally attached a connection error handler to the mongoose instance and no errors are reported.

Example:

const appt = new Appointment();
// fill with data here

appt.save((err, doc) => {
    // callback never called
});

const appt2 = new Appointment();
// fill with data here

appt2.$__save({}, (err, doc) => {
    // callback is called
});

For now I will use the private method as I’m not sure of how to proceed otherwise, but if I can give any more information to help with this, please let me know.

Update Similar to @mtimofiiv, console.log-ing the methods $__save and save give me the actual $__save method as defined in model.js and a function called wrappedPointCut, respectively.

Additionally, after modifying my installed version of mongoose (to log when methods are called) and re-running the code, I can see that indeed the $__save method reports that it is being called, while similar logging for save never reports.

Read more comments on GitHub >

github_iconTop Results From Across the Web

MongooseError [ParallelSaveError]: Can't save() the same doc ...
You have to create new instance of League each time. ... won't work inside forEach; You are calling res.send() every time you call...
Read more >
Exception handling (Task Parallel Library) - Microsoft Learn
Explore exception handling using the Task Parallel Library (TPL) in .NET. See nested aggregate exceptions, inner exceptions, unobserved task ...
Read more >
Pipeline: Basic Steps - Jenkins
The behavior of the step when an exception is thrown can be configured to print a message, set a build result other than...
Read more >
Handling Error Conditions Using a Step Functions State ...
Ensure that your Lambda function is under the same AWS account and AWS Region as your state machine. Open the AWS Lambda console...
Read more >
Top 10 Most Common Node.js Developer Mistakes - Toptal
js developers. Mistake #6: Throwing Errors from Inside Callbacks. JavaScript has the notion of exceptions. Mimicking the syntax of almost all traditional ...
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