Mongoose: after the latest update I can't get mongoose to work neither on windows 8 or on the server

Created on 24 Apr 2015  路  21Comments  路  Source: Automattic/mongoose

module.js:338
    throw err;
          ^
Error: Cannot find module 'mongodb/node_modules/mongodb-core/node_modules/bson/lib/bson/objectid'
    at Function.Module._resolveFilename (module.js:336:15)
    at Function.Module._load (module.js:278:25)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at Object.<anonymous> (C:\Users\Pedro\Documents\mkt\node_modules\mongoose\lib\drivers\node-mongodb-native\objectid.js:9:16)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Module.require (module.js:365:17)
can't reproduce

Most helpful comment

That's strange. Try doing an npm cache clear and a clean npm install rm -rf node_modules && npm install. What version of mongoose is this happening with?

All 21 comments

That's strange. Try doing an npm cache clear and a clean npm install rm -rf node_modules && npm install. What version of mongoose is this happening with?

latest

I went back to 4.0.1 and now it works

Are there any errors when you do npm install?

I am getting a different error, but also only on 4.0.2.

/Users/brandon/Documents/crm-server/node_modules/mongoose/lib/document.js:1846
        branch[part] = clone(self.get(path), options);
                     ^
TypeError: Cannot set property 'id' of undefined
    at applyGetters (/Users/brandon/Documents/crm-server/node_modules/mongoose/lib/document.js:1846:22)
    at model.Document.toObject (/Users/brandon/Documents/crm-server/node_modules/mongoose/lib/document.js:1741:5)
    at model.<anonymous> (/Users/brandon/Documents/crm-server/config/mongoose.js:17:29)
    at model.<anonymous> (/Users/brandon/Documents/crm-server/node_modules/mongoose/lib/schema.js:674:10)
    at model._next (/Users/brandon/Documents/crm-server/node_modules/mongoose/node_modules/hooks-fixed/hooks.js:62:30)
    at model.proto.(anonymous function) [as $__original_init] (/Users/brandon/Documents/crm-server/node_modules/mongoose/node_modules/hooks-fixed/hooks.js:108:20)
    at model.wrappedPointCut [as init] (/Users/brandon/Documents/crm-server/node_modules/mongoose/lib/document.js:1559:21)
    at completeOne (/Users/brandon/Documents/crm-server/node_modules/mongoose/lib/query.js:1466:10)
    at Object.<anonymous> (/Users/brandon/Documents/crm-server/node_modules/mongoose/lib/query.js:1201:13)
    at Object._onImmediate (/Users/brandon/Documents/crm-server/node_modules/mongoose/node_modules/mquery/lib/utils.js:137:16)
    at processImmediate [as _immediateCallback] (timers.js:354:15)

Reverting to 4.0.1 resolves the issue.

@brandom please provide code that reproduces this error.

@vkarpov15 I've been working on it. My app is rather large with a lot of bootstrap happening with the models. This happens somewhere in the bootstrap so I am only adding plugins and instantiating the models.

That would be great. Stack traces are better than nothing but they're of limited use because they don't really provide any context.

I'm receiving the same error that @brandom has received. Mongoose v3.8.24. Node.js v6.7.0.
Unfortunately I can't upgrade my Mongoose version due to compatibility issues. Is there any workaround for it?

/home/reasy/routeasy/routeasyweb/node_modules/mongoose/lib/document.js:1622
        branch[part] = clone(self.get(path), options);
                     ^

TypeError: Cannot set property '_id' of undefined
    at applyGetters (/home/reasy/routeasy/routeasyweb/node_modules/mongoose/lib/document.js:1622:22)
    at EmbeddedDocument.Document.toObject (/home/reasy/routeasy/routeasyweb/node_modules/mongoose/lib/document.js:1527:5)
    at EmbeddedDocument.Document.toJSON (/home/reasy/routeasy/routeasyweb/node_modules/mongoose/lib/document.js:1662:15)
    at clone (/home/reasy/routeasy/routeasyweb/node_modules/mongoose/lib/utils.js:224:18)
    at cloneArray (/home/reasy/routeasy/routeasyweb/node_modules/mongoose/lib/utils.js:305:14)
    at clone (/home/reasy/routeasy/routeasyweb/node_modules/mongoose/lib/utils.js:220:12)
    at cloneObject (/home/reasy/routeasy/routeasyweb/node_modules/mongoose/lib/utils.js:288:13)
    at clone (/home/reasy/routeasy/routeasyweb/node_modules/mongoose/lib/utils.js:233:16)
    at cloneObject (/home/reasy/routeasy/routeasyweb/node_modules/mongoose/lib/utils.js:288:13)
    at clone (/home/reasy/routeasy/routeasyweb/node_modules/mongoose/lib/utils.js:233:16)

@williamknn IIRC this was very specific to 4.0.2 for me. Looks like #2908 gets to the heart of the issue and the fix was in cb975db. Perhaps simply adding the appropriate bson version as a direct dependency of your project would fix it? Can't remember enough about module resolution in node v6 at this time to say for sure, but it's worth a quick experiment.

@brandom Thanks for your suggestion, but I've found the problem already. The crash has occurred after I made a merge with two branches. I looked up into the code and found the query that someone's have wrote and it's not supported in our Mongoose version.
This is the query:
Users.find({ "userInfo.subscriptions": { $exists: true, $not: { $size: 0 } } }, { "userInfo": 1 })

I haven't found the $not operator in Mongoose 3.8.x docs, so I think this was the problem. When I deleted the query, everything started to work again.

@williamknn that's strange, I can't think of a reason why that query wouldn't work in mongoose 5. Can you show your schemas please?

@vkarpov15 I've deleted some data because the schema is too long, and my Mongoose version is the 3.8.24:
https://pastebin.com/Fp93tDdh

What's preventing you from upgrading? 3.8.24 is 3 years old.

Too many deprecated functions. I've tried already but when I saw that is a job for a whole day (being VERY optimistic) I've gave up. Our application was constructed over MEAN.js v0.3.3 (from July of 2015) and there are a lot of things that may need to upgrade too when grow the Mongoose version.

I'm scheduling a week to update all the stuff we need to upgrade but for now, I've too many things on my backlog :/

That's reasonable, we'll investigate and see if we can find what's causing this issue

@williamknn looks like the pastebin is gone?

@vkarpov15 it has expired, I made a new one: https://pastebin.com/0p6pvhdg

Thanks, I'll make sure to look this week before it expires

@vkarpov15 I made this pastebin without an expire date ;)

@williamknn so I took a look and ran your schema against mongoose 5 and I can't see any reason why your schema wouldn't work with mongoose 5. Can you provide more info about what issues you're having with upgrading?

const mongoose = require('mongoose');
mongoose.set('debug', true);

const { Schema } = mongoose;

function validateLocalStrategyProperty() {
  return true;
}
function validateLocalStrategyPassword() {
  return true;
}

var UserSchema = new Schema({
    firstName: {
        type: String,
        trim: true,
        default: '',
        validate: [validateLocalStrategyProperty, 'Please fill in your first name']
    },
    lastName: {
        type: String,
        trim: true,
        default: '',
        validate: [validateLocalStrategyProperty, 'Please fill in your last name']
    },
    displayName: {
        type: String,
        trim: true
    },
    email: {
        type: String,
        trim: true,
        default: '',
        validate: [validateLocalStrategyProperty, 'Please fill in your email'],
        match: [/.+\@.+\..+/, 'Please fill a valid email address'],
        unique: 'E-mail already exists'
    },
    email_verified: {
        type: Boolean,
        default: true
    },
    document: {
        type: Number,
        trim: true
    },
    username: {
        type: String,
        unique: 'Username already exists',
        required: 'Please fill in a username',
        trim: true
    },
    password: {
        type: String,
        default: '',
        validate: [validateLocalStrategyPassword, 'Password should be longer']
    },
    salt: {
        type: String
    },
    provider: {
        type: String,
        required: 'Provider is required'
    },
    providerData: {},
    additionalProvidersData: {},
    roles: {
        type: [{
            type: String,
            enum: ['user', 'operator']
        }],
        default: ['user']
    },
    updated: {
        type: Date
    },
    created: {
        type: Date,
        default: Date.now
    },
    routingProfile: {
        type: { type: String },
        limit:Number,
        opts: mongoose.Schema.Types.Mixed
    },
    /* For reset password */
    resetPasswordToken: {
        type: String
    },
    resetPasswordExpires: {
        type: Date
    },
    plan: {
        id: { type: mongoose.Schema.ObjectId, ref: 'Plan'},
        name:{ type:String}
    },
    preferences: {
        map: String,
        consolidated: { type: Boolean, default: false },
        fuel_cost: {
            gasoline: {type: Number, default: 3.50},
            additive_gasoline: {type: Number, default: 3.90},
            ethanol: {type: Number, default: 2.90},
            diesel: {type: Number, default: 3.00},
            cng: {type: Number, default: 2.00}
        },
        tracking: {
            initialStatus: {
                type: String,
                default: 'operating'
            },
            updateTime: {
                type: Number,
                default: 60000
            },
            geolocation_accuracy: {
                type: Boolean,
                default: false
            },
            notify_user: {
                type: Boolean,
                default: false
            },
            email_template:{
                type: String,
                default: 'light'
            }
        },
        delivery_confirmation:{
            general: {
                type: Boolean,
                default: true
            },
            name_document: {
                type: Boolean,
                default: true
            },
            digital_signature: {
                type: Boolean,
                default: false
            },
            photo: {
                type: Boolean,
                default: false
            },
            comments: {
                type: Boolean,
                default: true
            },
        },
        deliveries: {
            start_time: {
                type: Date,
                default: new Date(1970, 0, 1, 6, 0, 0, 0)
            },
            end_time: {
                type: Date,
                default: new Date(1970, 0, 1, 21, 0, 0, 0)
            },
            service_time: {
                type: Number,
                default: 20
            }
        },
    },
    billing: { type: mongoose.Schema.ObjectId, ref: 'Billing' },
    userInfo: {
        customer_id: String,
        company: String,
        document: String,
        phone: String,
        website: String,
        logo: String,
        subscription_id: String,
        subscription_status: String,
        current_period_end : Date,
        onboarding: Boolean,
        subscriptions: [{
            id: String,
            plan_id: String,
            expiration_date: Date,
            status: String
        }],
        external: [{
            name: String,
            endpoint: String,
            auth: String
        }]
    },
    trial: {
        active: {
            type: Boolean,
            default: false
        },
        max_num_of_deliveries: Number,
        max_num_of_vehicles: Number,
        start_date: {
            type: Date
        },
        end_date: {
            type: Date
        }
    },
    operator: { type: mongoose.Schema.ObjectId, ref: 'Operator' },
    operators: [ { type: mongoose.Schema.ObjectId, ref: 'Operator'} ],
    advance_invoice: {
        required: {
            type: Boolean,
            default: false
        },
        : {
            type: Number,
            default: 21
        }
    },
    api_token: String,
    api_token_ts: Number
});

const User = mongoose.model('User', UserSchema);
mongoose.connect('mongodb://localhost:27017/test');

run().catch(error => console.error(error.stack));

async function run() {
  await User.create({ provider: 'test', username: 'vkarpov15' });
  console.log('done');
}
Was this page helpful?
0 / 5 - 0 ratings