buunguyen / mongoose-deep-populate

Mongoose plugin to enable deep population of nested models ⛺
MIT License
469 stars 44 forks source link

undefined is not a function\n #26

Closed eduolalo closed 9 years ago

eduolalo commented 9 years ago

I did upgrade my mongoose module to v4.1.6, the upgrade mongoose-deep-populate and followed the instructions to register the plugin, then it throws me the this error in a code that was working in the v1.0.0 of this module

Error: TypeError: undefined is not a function\n at Object.module.exports.checkUser (/Users/christian/project/project-backend/lib/handler.js:70:10)

this is the code:

User.findOne({
            key: value
})
 .deepPopulate('card.spendLimit card.spendLimit.thisDay card spendingCoach type company useType') // this is the 70th line
 .exec(function(err, user) {
   //...
})
buunguyen commented 9 years ago

Did you look at this? https://github.com/buunguyen/mongoose-deep-populate#v200

eduolalo commented 9 years ago

Yes, I'm passing the mongoose instance

buunguyen commented 9 years ago

Please post full code + stack trace. Would be great if it's executable to reproduce the error.

eduolalo commented 9 years ago

Model

var mongoose = require('mongoose');

var DeepPopulate = require('mongoose-deep-populate')(mongoose);

var Schema = mongoose.Schema;

var schema = {
    //...
};

var UserSchema =  new Schema(schema);

UserSchema.plugin(DeepPopulate);

module.exports = mongoose.model('User', UserSchema);

Code

User.findOne({
    token: req.args.token
})
.deepPopulate('card.spendLimit card.spendLimit.thisDay card spendingCoach type company useType')
.exec(function(userErr, user) {
    //...
});

Stack

TypeError: undefined is not a function\n    at Object.module.exports.checkUser (/Users/christian/debito/debito-backend/lib/handler.js:70:10)\n    at Object.module.exports.preread (/Users/christian/debito/debito-backend/lib/handler.js:120:18)\n    at Layer.handle [as handle_request] (/Users/christian/debito/debito-backend/node_modules/express/lib/router/layer.js:82:5)\n    at next (/Users/christian/debito/debito-backend/node_modules/express/lib/router/route.js:100:13)\n    at Route.dispatch (/Users/christian/debito/debito-backend/node_modules/express/lib/router/route.js:81:3)\n    at Layer.handle [as handle_request] (/Users/christian/debito/debito-backend/node_modules/express/lib/router/layer.js:82:5)\n    at /Users/christian/debito/debito-backend/node_modules/express/lib/router/index.js:235:24\n    at Function.proto.process_params (/Users/christian/debito/debito-backend/node_modules/express/lib/router/index.js:313:12)\n    at /Users/christian/debito/debito-backend/node_modules/express/lib/router/index.js:229:12\n    at Function.match_layer (/Users/christian/debito/debito-backend/node_modules/express/lib/router/index.js:296:3)\n    at next (/Users/christian/debito/debito-backend/node_modules/express/lib/router/index.js:190:10)\n    at /Users/christian/debito/debito-backend/node_modules/express/lib/router/index.js:192:16\n    at Function.match_layer (/Users/christian/debito/debito-backend/node_modules/express/lib/router/index.js:296:3)\n    at next (/Users/christian/debito/debito-backend/node_modules/express/lib/router/index.js:190:10)\n    at /Users/christian/debito/debito-backend/node_modules/express/lib/router/index.js:192:16\n    at Function.match_layer (/Users/christian/debito/debito-backend/node_modules/express/lib/router/index.js:296:3)\n    at next (/Users/christian/debito/debito-backend/node_modules/express/lib/router/index.js:190:10)\n    at /Users/christian/debito/debito-backend/node_modules/express/lib/router/index.js:192:16\n    at Function.match_layer (/Users/christian/debito/debito-backend/node_modules/express/lib/router/index.js:296:3)\n    at next (/Users/christian/debito/debito-backend/node_modules/express/lib/router/index.js:190:10)\n    at /Users/christian/debito/debito-backend/index.js:116:9\n    at Layer.handle [as handle_request] (/Users/christian/debito/debito-backend/node_modules/express/lib/router/layer.js:82:5)
eduolalo commented 9 years ago

Is not working when I try to chain as query, but it does when I use it as model statics

buunguyen commented 9 years ago

If you pass in the right instance of mongoose used by the app, I can't think of a reason why deepPopulate isn't available in Query. It's not clear from the stack trace either (which BTW, you should have formatted more properly before posting). I suggest you debug your code, if you're sure it's a bug of the plugin, I'll take a look.