Mongoose: ParallelSaveError: Can't save() the same doc multiple times in parallel

Created on 21 Jul 2019  路  1Comment  路  Source: Automattic/mongoose

Do you want to request a feature or report a bug?
bug

What is the current behavior?

I recently found that mongoose does not allow a document to be saved if a previous save hasn't completed yet. Makes total sense. That being said, the ParallelSaveError does not get caught in a try/catch.

try {
    document.save()
    document.save()
} catch(err) {
    console.log("There was an error")
    console.log(err)
}

"There was an error" never gets logged.
What is the expected behavior?
The catch should catch the ParallelSaveError and "There was an error" should be logged.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.

Mongoose: 5.6.5
Node.js: 10.15.2

help won't fix

>All comments

ParallelSaveError must be async, because Mongoose checks for parallel saves after pre hooks run. You should await on save(), and then you can try/catch the error.

try {
    await document.save()
    await document.save()
} catch(err) {
    console.log("There was an error")
    console.log(err)
}
Was this page helpful?
0 / 5 - 0 ratings