Calling a static function returns "Not a function",

I am working on a stack.js application. When I try to call a static function of one of my models, I get the error message "SomeItem.createNew is not a function"

I call this static method from another static method of another circuit, for example.

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 it 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 put the requirement inside a static function, it works fine. Why is this? I would only like to declare “var SomeItem” only once at the top, and not in every function that I need to use.

+4
source share
1 answer

, , , , , .

, , , , - . - , , , , , .

.

0

Source: https://habr.com/ru/post/1672935/


All Articles