mongoose: Deep cloning a mongoose model with arrays inside doesn't work after #6431

I’m using MongoDB 3.6.5 Community, NodeJS 9.11.1 and Mongoose 5.1.2, on Ubuntu 16.04.

Before the changes introduced in this commit to fix #6431, it was possible to do a

_.deepClone(mongooseModel)

where _ is lodash 4.11.1 and mongooseModel is a document that holds an array as one of its properties in the schema.

The error shown in the console looks like the following:

(node:58559) UnhandledPromiseRejectionWarning: TypeError: Class constructor CoreMongooseArray cannot be invoked without ‘new’

Would it be possible to change the way CoreMongooseArray has been implemented in order to make it easier to deep clone?

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 18 (2 by maintainers)

Most upvoted comments

@valterkraemer I was able to reproduce with lodash 4.17.4 and confirm that 4.17.10 fixes it. Thanks for reporting this, I’m glad it’s sorted out.

Is there a unit test for this? I’m seeing the same issue now with mongoose@6.1.4 and lodash@4.17.21

Using MongoDB 3.6.3, Mongoose 5.1.2, Lodash 4.17.4 I get the Error Unhandled rejection TypeError: Class constructor CoreMongooseArray cannot be invoked without 'new'. But after updating Lodash to 4.17.10 I don’t get the error anymore.

const mongoose = require('mongoose')
mongoose.Promise = require('bluebird')
const Schema = mongoose.Schema
const _ = require('lodash')

mongoose.connect('mongodb://localhost/test')

const UserSchema = new Schema({
  name: String,
  items: [{
    name: String
  }]
})

const User = mongoose.model('User', UserSchema)

return User.create({
  name: 'Arnold',
  items: [{
    name: 'Item 1'
  }]
})
.then(user => {
  const copy = _.cloneDeep(user)
})

Thank you all for your time. As @valterkraemer and @lineus reported, upgrading lodash to 4.17.10 fixed the problem.