I'm working on a mean.js stack application. When I attempt to call a static function of one of my models I'm getting the error 'SomeItem.createNew is not a function'
I am calling this static method from another static method of a different schema, like so.
Does NOT work:
var SomeItem = require('./some-item.js');
aSchema.statics.createNew = function(body, cb) {
var newA = new this();
//create child items
for (i = 0; i < body.someItems.length; i++ {
SomeItem.createNew(body.someItems[i], function(err, item) {
}
}
Does work:
aSchema.statics.createNew = function(body, cb) {
var newA = new this();
var SomeItem = require('./some-item.js');
//create child items
for (i = 0; i < body.someItems.length; i++ {
SomeItem.createNew(body.someItems[i], function(err, item) {
}
}
However if I place the require inside of the static function it works fine. Why is this? I would like to only have to declare 'var SomeItem' only once at the top, not in every function I need to use it.