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

_markModified is not a function (resurfaced in 4.9.4) #5162

Closed
bertrandmartel opened this issue Apr 11, 2017 · 1 comment
Closed

_markModified is not a function (resurfaced in 4.9.4) #5162

bertrandmartel opened this issue Apr 11, 2017 · 1 comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@bertrandmartel
Copy link

I'm having the following issue which is this resurfaced issue but with the latest mongoose version 4.9.4

/home/user/Desktop/node_modules/mongoose/lib/types/embedded.js:87
    this.__parentArray._markModified();
                       ^

TypeError: this.__parentArray._markModified is not a function
    at EmbeddedDocument.markModified (/home/user/Desktop/node_modules/mongoose/lib/types/embedded.js:87:24)
    at SingleNested.Subdocument.markModified (/home/user/Desktop/node_modules/mongoose/lib/types/subdocument.js:62:18)
    at SingleNested.Document.$__set (/home/user/Desktop/node_modules/mongoose/lib/document.js:874:10)
    at SingleNested.Document.set (/home/user/Desktop/node_modules/mongoose/lib/document.js:785:10)
    at SingleNested._handleIndex (/home/user/Desktop/node_modules/mongoose/lib/document.js:625:14)
    at SingleNested.Document.set (/home/user/Desktop/node_modules/mongoose/lib/document.js:585:24)
    at SchemaType.Embedded.cast (/home/user/Desktop/node_modules/mongoose/lib/schema/embedded.js:132:12)
    at SchemaType.getDefault (/home/user/Desktop/node_modules/mongoose/lib/schematype.js:616:23)
    at EmbeddedDocument.Document.$__buildDoc (/home/user/Desktop/node_modules/mongoose/lib/document.js:265:22)
    at EmbeddedDocument.Document (/home/user/Desktop/node_modules/mongoose/lib/document.js:61:20)
    at EmbeddedDocument [as constructor] (/home/user/Desktop/node_modules/mongoose/lib/types/embedded.js:31:12)
    at new EmbeddedDocument (/home/user/Desktop/node_modules/mongoose/lib/schema/documentarray.js:70:17)
    at DocumentArray.SchemaArray (/home/user/Desktop/node_modules/mongoose/lib/schema/array.js:67:21)
    at new DocumentArray (/home/user/Desktop/node_modules/mongoose/lib/schema/documentarray.js:31:13)
    at Function.Schema.interpretAsType (/home/user/Desktop/node_modules/mongoose/lib/schema.js:618:14)
    at Schema.path (/home/user/Desktop/node_modules/mongoose/lib/schema.js:563:29)

The code is the following

"use strict";

var mongoose = require('mongoose');

var db = mongoose.createConnection("mongodb://localhost:27017/testDB");

let RatingsItemSchema = new mongoose.Schema({
    id: Number,
    value: Number,
    _id: false
});

let RatingsItem = db.model('RatingsItem', RatingsItemSchema);

let RatingsSchema = new mongoose.Schema({
    ratings: {
        type: RatingsItemSchema,
        default: 
            { id: 1, value: 0 }
        
    },
    _id: false
});

let RestaurantSchema = new mongoose.Schema({
    ratings: {
        type: [RatingsItemSchema],
        default: [
            { id: 1, value: 0 },
            { id: 2, value: 0 },
            { id: 3, value: 0 },
            { id: 4, value: 0 },
            { id: 5, value: 0 }
        ]
    },
    menu: {
        type: [RatingsSchema]
    }
});

let Ratings = db.model('Ratings', RatingsSchema);
let Restaurant = db.model('Restaurant', RestaurantSchema);

var rest = new Restaurant();
rest.menu.push(new Ratings());
console.log(JSON.stringify(rest, null, 2));

Version

mongoose 4.9.4 
mongodb 3.4.2
node 5.10.0

The workaround specified in the beginning of referenced issue fix this issue (adding || !this.__parentArray._markModified to the prototype in node_modules/mongoose/lib/types/embedded.js)


Furthermore, I've discovered the issue caused another issue (not sure if thats a separate issue), I have got the following error

/home/user/Desktop/node_modules/mongoose/lib/schema/documentarray.js:322
            throw new CastError('embedded', valueInErrorMessage,
            ^
CastError: Cast to embedded failed for value "{ id: 5, value: 0 }" at path "ratings"
    at MongooseError.CastError (/home/user/Desktop/node_modules/mongoose/lib/error/cast.js:26:11)
    at DocumentArray.cast (/home/user/Desktop/node_modules/mongoose/lib/schema/documentarray.js:322:19)
    at DocumentArray.SchemaType.getDefault (/home/user/Desktop/node_modules/mongoose/lib/schematype.js:616:23)
    at EmbeddedDocument.Document.$__buildDoc (/home/user/Desktop/node_modules/mongoose/lib/document.js:265:22)
    at EmbeddedDocument.Document (/home/user/Desktop/node_modules/mongoose/lib/document.js:61:20)
    at EmbeddedDocument [as constructor] (/home/user/Desktop/node_modules/mongoose/lib/types/embedded.js:31:12)
    at new EmbeddedDocument (/home/user/Desktop/node_modules/mongoose/lib/schema/documentarray.js:70:17)
    at DocumentArray.SchemaArray (/home/user/Desktop/node_modules/mongoose/lib/schema/array.js:67:21)
    at new DocumentArray (/home/user/Desktop/node_modules/mongoose/lib/schema/documentarray.js:31:13)
    at Function.Schema.interpretAsType (/home/user/Desktop/node_modules/mongoose/lib/schema.js:618:14)
    at Schema.path (/home/user/Desktop/node_modules/mongoose/lib/schema.js:563:29)
    at Schema.add (/home/user/Desktop/node_modules/mongoose/lib/schema.js:445:12)
    at new Schema (/home/user/Desktop/node_modules/mongoose/lib/schema.js:99:10)
    at Object.<anonymous> (/home/user/Desktop/mongoose.js:38:24)
    at Module._compile (module.js:413:34)
    at Object.Module._extensions..js (module.js:422:10)

Which occurs when I specify a nested array instead of a nested object and setting a default value for this array. The code is the following

"use strict";

var mongoose = require('mongoose');

var db = mongoose.createConnection("mongodb://localhost:27017/testDB");

let RatingsItemSchema = new mongoose.Schema({
    id: Number,
    value: Number,
    _id: false
});

let RatingsItem = db.model('RatingsItem', RatingsItemSchema);

let RatingsSchema = new mongoose.Schema({
    ratings: {
        type: [RatingsItemSchema],
        default: [
            { id: 1, value: 0 },
            { id: 2, value: 0 },
            { id: 3, value: 0 },
            { id: 4, value: 0 },
            { id: 5, value: 0 }
        ]
    },
    _id: false
});

let RestaurantSchema = new mongoose.Schema({
    ratings: {
        type: [RatingsItemSchema],
        default: [
            { id: 1, value: 0 },
            { id: 2, value: 0 },
            { id: 3, value: 0 },
            { id: 4, value: 0 },
            { id: 5, value: 0 }
        ]
    },
    menu: {
        type: [RatingsSchema]
    }
});

let Ratings = db.model('Ratings', RatingsSchema);
let Restaurant = db.model('Restaurant', RestaurantSchema);

var rest = new Restaurant();
rest.menu.push(new Ratings());
console.log(JSON.stringify(rest, null, 2));

@sobafuchs
Copy link
Contributor

sobafuchs commented Apr 18, 2017

yup, looks like the default value for an array is what causes the issue:

const mongoose = require('mongoose');
const co = require('co');
mongoose.Promise = global.Promise;
const GITHUB_ISSUE = `gh-5162`


exec()
  .then(() => {
    console.log('successfully ran program');
    process.exit(0);
  })
  .catch(error => {
    console.error(`Error: ${error}\n${error.stack}`);
  });


function exec() {
  return co(function* () {
    const db = mongoose.createConnection(`mongodb://localhost:27017/${GITHUB_ISSUE}`)
    let RatingsItemSchema = new mongoose.Schema({
      id: Number,
      value: Number,
      _id: false
    });

    let RatingsItem = db.model('RatingsItem', RatingsItemSchema);

    let RatingsSchema = new mongoose.Schema({
      ratings: {
        type: RatingsItemSchema,
        default:
        { id: 1, value: 0 }
      },
      _id: false
    });

    let RestaurantSchema = new mongoose.Schema({
      ratings: {
        type: [RatingsItemSchema],
        default: [
          { id: 1, value: 0 },
          { id: 2, value: 0 },
          { id: 3, value: 0 },
          { id: 4, value: 0 },
          { id: 5, value: 0 }
        ]
      },
      menu: {
        type: [RatingsSchema]
      }
    });

    let Ratings = db.model('Rating', RatingsSchema);
    let Restaurant = db.model('Restaurant', RestaurantSchema);

    var rest = new Restaurant();
    rest.menu.push(new Ratings());
    console.log(JSON.stringify(rest, null, 2));
  });
}

@sobafuchs sobafuchs added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Apr 18, 2017
@sobafuchs sobafuchs added this to the 4.9.6 milestone Apr 18, 2017
vkarpov15 added a commit that referenced this issue Apr 20, 2017
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

2 participants