Mongoose: Error when calling unshift() after map()

Created on 16 May 2020  路  2Comments  路  Source: Automattic/mongoose

This is almost the same issue as #8399 (root cause seems to be #8356).

Here is a repro:

const MyModel = db.model('Test', Schema({
  myArray: [{ name: String }]
}));

const doc = new MyModel({
  myArray: [{ name: 'b' }, { name: 'c' }]
});
let myArray = doc.myArray;

myArray = myArray.map(val => ({ name: `${val.name} mapped` }));

myArray.unshift({ name: 'a inserted' });

assert.deepEqual(myArray.map(v => v.name), [
  'a inserted',
  'b mapped',
  'c mapped'
]);

I'll create a PR in a minute for this issue.

confirmed-bug

All 2 comments

Output for script above

TypeError: Cannot read property 'applySetters' of null
    at CoreDocumentArray.unshift (mongoose\lib\types\core_array.js:893:38)
    at Object.<anonymous> (mongoose\test.js:17:9)

Thanks @AbdelrahmanHafez, forgot to include the error, will do next time 馃憤

Was this page helpful?
0 / 5 - 0 ratings