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

Schema Inheritance Error (TypeError: Cannot read property '0' of undefined) #56

Open
xulong3370 opened this issue May 10, 2017 · 11 comments

Comments

@xulong3370
Copy link

When I try schema inheritance :

var mongoose = require('mongoose'),
    extend = require('mongoose-schema-extend');
var Schema = mongoose.Schema;

var PersonSchema = new Schema({
    name : String
}, { collection : 'users' });

var EmployeeSchema = PersonSchema.extend({
    department : String
});

var Person = mongoose.model('Person', PersonSchema),
    Employee = mongoose.model('Employee', EmployeeSchema);

I get the following error:

"E:\Program Files\JetBrains\WebStorm 2017.1.1\bin\runnerw.exe" "C:\Program Files\nodejs\node.exe" D:\Users\test_extend\test.js
D:\Users\test_extend\node_modules\mongoose\lib\services\model\applyHooks.js:34
if (pair[0] !== 'pre' && pair[0] !== 'post' && pair[0] !== 'on') {
^

TypeError: Cannot read property '0' of undefined
at applyHooks (D:\Users\test_extend\node_modules\mongoose\lib\services\model\applyHooks.js:34:13)
at Function.compile (D:\Users\test_extend\node_modules\mongoose\lib\model.js:3636:3)
at Mongoose.model (D:\Users\test_extend\node_modules\mongoose\lib\index.js:417:22)
at Object. (D:\Users\test_extend\test.js:17:25)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:393:7)
at startup (bootstrap_node.js:150:9)
at bootstrap_node.js:508:3

Process finished with exit code 1

It looks like the following code cause the error:

var EmployeeSchema = PersonSchema.extend({
    department : String
});

 Employee = mongoose.model('Employee', EmployeeSchema);`
@Climax777
Copy link

Same for me. Upgraded from mongoose 4.9.1 to 4.9.8

@Climax777
Copy link

@xulong3370 have you been able to find a workaround?

@xulong3370
Copy link
Author

@Climax777 not resolve so far....

@mwlvandermaat
Copy link

Still no workaround?

@Climax777
Copy link

@mwlvandermaat not that I know of

@pcemma
Copy link

pcemma commented Jun 26, 2017

Is there a solution for this issue?

@sde-turner
Copy link

sde-turner commented Jul 11, 2017

I confirm I am facing a similar issue.

For what it's worth the last working Mongoose version I can find with 0.2.2 of mongooses-schema-extend that is working is 4.9.7

@amorimdub
Copy link

I got the same error.

My mistake was: I was initializing the child's model before father's model.

Please, double check which one is load first.

@pcemma
Copy link

pcemma commented Oct 2, 2017

Nope. It's still not working. I'm sure that parent model is initialized before children one.

@hoangnguyenba
Copy link

Still not fixed :(

@InsomniakDev
Copy link

almost one year later still not resolved :'(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants