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

Created on 22 Jun 2018  路  1Comment  路  Source: Automattic/mongoose

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

What is the current behavior?
I get this error:

{ ParallelSaveError: Can't save() the same doc multiple times in parallel. Document: 5b2c9a3276254b3c471396c2
    at ParallelSaveError.MongooseError [as constructor] (/home/lally/workspace/mongoose/playground/node_modules/mongoose/lib/error/mongooseError.js:11:11)
    at new ParallelSaveError (/home/lally/workspace/mongoose/playground/node_modules/mongoose/lib/error/parallelSave.js:18:17)
    at model.Model.save (/home/lally/workspace/mongoose/playground/node_modules/mongoose/lib/model.js:342:23)
    at toExecute.push.callback (/home/lally/workspace/mongoose/playground/node_modules/mongoose/lib/model.js:2244:16)
    at /home/lally/workspace/mongoose/playground/node_modules/async/internal/parallel.js:31:39
    at eachOfArrayLike (/home/lally/workspace/mongoose/playground/node_modules/async/eachOf.js:65:9)
    at exports.default (/home/lally/workspace/mongoose/playground/node_modules/async/eachOf.js:9:5)
    at _parallel (/home/lally/workspace/mongoose/playground/node_modules/async/internal/parallel.js:30:5)
    at parallelLimit (/home/lally/workspace/mongoose/playground/node_modules/async/parallel.js:88:26)
    at utils.promiseOrCallback.cb (/home/lally/workspace/mongoose/playground/node_modules/mongoose/lib/model.js:2248:5)
    at Object.promiseOrCallback (/home/lally/workspace/mongoose/playground/node_modules/mongoose/lib/utils.js:222:14)
    at Function.create (/home/lally/workspace/mongoose/playground/node_modules/mongoose/lib/model.js:2209:16)
    at Object.<anonymous> (/home/lally/workspace/mongoose/playground/parallel.save.js:31:6)
    at Module._compile (internal/modules/cjs/loader.js:678:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:689:10)
    at Module.load (internal/modules/cjs/loader.js:589:32)
  message: 'Can\'t save() the same doc multiple times in parallel. Document: 5b2c9a3276254b3c471396c2',
  name: 'ParallelSaveError' }

If the current behavior is a bug, please provide the steps to reproduce.

#!/usr/bin/env node

'use strict';

process.env.MONGODB_URI =
    (process.env.MONGODB_URI || 'mongodb://localhost/mongoose-playground');

/* dependencies */
const mongoose = require('mongoose');
const Schema = mongoose.Schema;


mongoose.connect(process.env.MONGODB_URI);

const NoteSchema = new Schema({
    body: String
});

NoteSchema.pre('save', function(next) {
    next();
});

NoteSchema.post('save', function(note, next) {
    note.save(function(error, saved) {
        next(error, saved);
    });
});

const Note = mongoose.model('Note', NoteSchema);

Note.create({ body: 'Quick note' }, function(error, note) {
    console.log(error);
    console.log(note);
});

What is the expected behavior?
Post save hook have to work as expected and not restricted with parallel saving checkings

Please mention your node.js, mongoose and MongoDB version.
node.js : v10.1.0
mongoose: v5.1.6
MongoDB: v3.4.10

confirmed-bug

Most helpful comment

@lykmapipo thanks for reporting this, and especially thanks for the concise repro script! I'll sort out a PR for this ASAP.

>All comments

@lykmapipo thanks for reporting this, and especially thanks for the concise repro script! I'll sort out a PR for this ASAP.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ArThoX picture ArThoX  路  3Comments

weisjohn picture weisjohn  路  3Comments

simonxca picture simonxca  路  3Comments

lukasz-zak picture lukasz-zak  路  3Comments

CodeurSauvage picture CodeurSauvage  路  3Comments