Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Cannot read property '$__' of undefined at _updateParentPopulated #8317

Closed
shaolintl opened this issue Nov 9, 2019 · 4 comments
Closed
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@shaolintl
Copy link

Hello, this seems similar to #3889.

I am using node 8.10.0, mongodb 3.2.11 and mongoose 5.7.9

I have the following code:

var forms = this.getFormalization();
var form = forms.find(f => f._id == id);
  if (typeof form.lastIndependentDate === 'undefined' || this.lastUpdate > 
      form.lastIndependentDate) {
    forms = forms.slice()
    forms.splice(forms.indexOf(form), 1)

The last line throws the error:

TypeError: Cannot read property '$__' of undefined
at _updateParentPopulated (/home/shaolin/Documents/software/normative-gui/node_modules/mongoose/lib/types/documentarray.js:323:14)
at CoreDocumentArray.splice (/home/shaolin/Documents/software/normative-gui/node_modules/mongoose/lib/types/documentarray.js:228:5)
at model.theorySchema.methods.isIndependent (/home/shaolin/Documents/software/normative-gui/models/theory.js:145:11)
at /home/shaolin/Documents/software/normative-gui/controllers/theoryController.js:202:14
at /home/shaolin/Documents/software/normative-gui/node_modules/mongoose/lib/model.js:4604:16
at /home/shaolin/Documents/software/normative-gui/node_modules/mongoose/lib/query.js:4351:12
at process.nextTick (/home/shaolin/Documents/software/normative-gui/node_modules/mongoose/lib/query.js:2849:28)
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)

The model is

var theorySchema = new Schema({
    lastUpdate        : Date,
    name 		          : String,
    description       : String,
    content           : String, // annotated XML
    comment           : String,
    vocabulary        : [{symbol: String, original: String, full: String}],
    autoVocabulary    : [{symbol: String, original: String, full: String}],
    formalization     : [{original: String, json: Object, formula: String, active: Boolean, lastIndependent: Boolean, lastIndependentDate: Date}],
    autoFormalization : [{original: String, json: Object, formula: String, active: Boolean, lastIndependent: Boolean, lastIndependentDate: Date}],
		user 							: { type: Schema.Types.ObjectId, ref: 'User' },
    clonedForm        : { type: Schema.Types.ObjectId, ref: 'Theory' },
    lastConsistency   : Boolean,
    lastConsistencyDate: Date,
    writeProtected    : Boolean
});


theorySchema.methods.getFormalization = function() {
  return this.formalization.concat(this.autoFormalization)
}

Downgrading to 4.4.3 also fixes it in my case.

Thank you.

@mcijsouw
Copy link

I'm running into the same issue after upgrading from 5.7.7 to 5.7.8!

@Geenzo
Copy link

Geenzo commented Nov 14, 2019

had the same issue going to 5.6.x - 5.7.10, workaround i found was not mutating the original array from the document
image

same issue as #8312

@esodell
Copy link

esodell commented Nov 14, 2019

Same issue going from 5.5.x to 5.7.11. Downgrading back to 5.7.7 fixed the issue

@vkarpov15 vkarpov15 added this to the 5.7.12 milestone Nov 16, 2019
@vkarpov15 vkarpov15 added the needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue label Nov 16, 2019
vkarpov15 added a commit that referenced this issue Nov 17, 2019
@vkarpov15 vkarpov15 added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue labels Nov 17, 2019
@AbdelrahmanHafez
Copy link
Collaborator

This is a bug with map(...) as well, creating a PR for it.

Repro script:

'use strict';
const mongoose = require('./');
const {Schema} = mongoose;

const assert = require('assert');

const personSchema = new Schema({
  friends: [{ name: { type: String } }]
});

const Person = mongoose.model('Person', personSchema);

const person = new Person({ friends: [{ name: 'Hafez' }] });

const friendsNames = person.friends.map(friend => friend.name);

let threw = false;

try {
  friendsNames.push('Sam');
} catch (err) {
  threw = true;
  console.log(err);
}

assert.equal(threw, false);
assert.equal(friendsNames.length, 2);
assert.equal(friendsNames[1], 'Sam');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

No branches or pull requests

6 participants