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

Mongoose 5.7.5 breaking cloning #8292

Closed
dawnmessier opened this issue Oct 30, 2019 · 0 comments
Closed

Mongoose 5.7.5 breaking cloning #8292

dawnmessier opened this issue Oct 30, 2019 · 0 comments
Labels
has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue
Milestone

Comments

@dawnmessier
Copy link

BUG

Using

  • NodeJs 12.12.0
  • Mongoose 5.7.5
  • MongoDb 3.3.3

I have a pre-existing module for cloning a schema which is used to back up web pages (like a CMS system). When I upgraded to mongoose 5.7.5 (from 5.5.2), the schema cloning broke.

this is the cloning module:

    module.exports = function (schema, mongoose) {
    'use strict';

    mongoose = mongoose || require('mongoose');

    let clonedSchema = new mongoose.Schema();

    schema.eachPath(function (key, path) {
        if (key === '_id') {
            return;
        }

        let clonedPath = {};

        clonedPath[key] = path.options;
        delete clonedPath[key].unique;

        clonedSchema.add(clonedPath);
    });

    return clonedSchema;
    };

this is the schema getting cloned

    let pageSchema = new Schema({
    title: {
        type: String,
        required: true
    },
    translateKey: {
        type: String,
        required: true
    },
    lang: {
        type: String,
        required: true
    }
    },
    {
    timestamps: true
    });

It's breaking on clonedSchema.add(clonedPath) with the first path it tries to add.

ERROR

    UnhandledPromiseRejectionWarning: MongooseError: Invalid ref at path "title". Got null
    at new MongooseError (/node_modules/mongoose/lib/error/mongooseError.js:10:11)
    at validateRef (/node_modules/mongoose/lib/helpers/populate/validateRef.js:17:9)
    at Clone.Schema.path (/node_modules/mongoose/lib/schema.js:577:5)
    at Clone.add (/node_modules/mongoose/lib/schema.js:442:12)
    at Schema.extend (/node_modules/mongoose-schema-extend/index.js:81:13)
    at /api/common/lib/clone-schema.js:19:22
    at Schema.eachPath (/api/node_modules/mongoose/lib/schema.js:934:5)
    at module.exports (/api/common/lib/clone-schema.js:9:12)
    at Object.<anonymous> (/api/common/schemas/Page.schema.js:39:23)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)

I did a comparison of the schema passed into this module between mongoose 5.5.2 and 5.7.5. The only difference in 5.7.5 is that $immutable is now an included property

    title: 
      SchemaString {
        enumValues: [],
        regExp: null,
        path: 'title',
        instance: 'String',
        validators: [Array],
        getters: [],
        setters: [],
        options: [Object],
        _index: null,
        '$immutable': null,
        isRequired: true,
        requiredValidator: [Function],
        originalRequiredValue: true,
        [Symbol(mongoose#schemaType)]: true },
     translateKey: 
      SchemaString {
        enumValues: [],
        regExp: null,
        path: 'translateKey',
        instance: 'String',
        validators: [Array],
        getters: [],
        setters: [],
        options: [Object],
        _index: null,
        '$immutable': null,
        isRequired: true,
        requiredValidator: [Function],
        originalRequiredValue: true,
        [Symbol(mongoose#schemaType)]: true }

I tried "npm mongoose-schema-extend", but that gave me TypeError: Method Map.prototype.has called on incompatible receiver [object Map]. So not sure how to use it here.

    const extend = require('mongoose-schema-extend');

    let clonedSchema = new mongoose.Schema();

    schema.eachPath(function (key, path) {
        if (key === '_id') {
            return;
        }

        let prop = {};
        prop[key] = path;
        clonedSchema.extend(prop);
    });

I threw a try/catch into the module to see if that would do anything. Nope.

I also deleted the node_modules directory and re-installed the plugins.

I also added useUnifiedTopology: true to Mongoose initialization for posterity.

Does anyone know what changed in 5.7 to break this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue
Projects
None yet
Development

No branches or pull requests

2 participants