mongoose: Cannot read property '$__original_save' of undefined

node 6.9.1 mongodb 2.2.22 (npm version) mongo 3.3 (actual db)

getting the following error on the latest tag (4.8.1):

Error: Cannot read property '$__original_save' of undefined
      at /var/app/current/node_modules/mongoose/lib/services/model/applyHooks.js:135:16
      at new promise (/var/app/current/node_modules/q/q.js:682:9)
      at wrappedPointCut (/var/app/current/node_modules/mongoose/lib/services/model/applyHooks.js:115:23)
      at _fulfilled (/var/app/current/node_modules/q/q.js:834:54)
      at self.promiseDispatch.done (/var/app/current/node_modules/q/q.js:863:30)
      at Promise.promise.promiseDispatch (/var/app/current/node_modules/q/q.js:796:13)
      at /var/app/current/node_modules/q/q.js:604:44
      at runSingle (/var/app/current/node_modules/q/q.js:137:13)
      at flush (/var/app/current/node_modules/q/q.js:125:13)

Locking my mongoose version to 4.7.5 fixes the issue. Am I missing something important? This happens when creating/saving something new.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 2
  • Comments: 16 (7 by maintainers)

Most upvoted comments

In this line:

async.parallel([newUser.save, newStudent.save], callback);

the save methods lose reference/context to the models. Instead, you’ll want to do:

async.parallel([newUser.save.bind(newUser), newStudent.save.bind(newStudent)], callback);

or

async.parallel([
    (callback) => newUser.save(callback),
    (callback) => newStudent.save(callback)
], callback);

I believe your version used to work at some point (early versions of v4), but that was incorrect/not-well-defined behaviour

@valera33 @tommarien be careful of losing function context, you’d get the same error if you did const fn = user.save; fn().then(res => {});. The correct way is either .then(() => user.save()) or .then(user.save.bind(user)), if you pass a member function as a parameter then function context goes away and internally user.save doesn’t know what this is.

the same issue as described by @kysemon please someone help. node v8.0.0 npm 5.0 mongoose 4.10.8

Cannot read property ‘$__original_save’ of undefined

module.exports.saveStudent = function(newUser, newStudent, callback){
  bcrypt.hash(newUser.password, 10, (err, hash)=>{
    if(err) throw err;
    newUser.password=hash;
    console.log("Student is saved");
    async.parallel([newUser.save, newStudent.save], callback);
  });
}