如何修复“类扩展未定义的值不是构造函数或null",NodeJS [英] How to fix 'Class extends value undefined is not a constructor or null' NodeJS

查看:299
本文介绍了如何修复“类扩展未定义的值不是构造函数或null",NodeJS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按以下顺序具有3个文件结构,所有文件结构都包含1个类

I have 3 files structure in the following order all of which contain 1 class

main.js extends events
events.js extends base
base.js

我已经研究了这些答案,但是我的问题似乎与以下任何人所描述的都不一样. TypeError:类扩展未定义的值不是函数或为空

I've looked into these answers but my problem doesn't appear to look like anything of the following people described. TypeError: Class extends value undefined is not a function or null

main.js

const { Events } = require('./events.js');

module.exports = class Main extends Events {
    constructor(token) {
        super();

        // Some code
    }
}

events.js

events.js

const { Base } = require('./base.js');

module.exports = class Events extends Base {
    constructor() {
        super();
    }

    // more code
}

base.js

module.exports = class Base{
    constructor() {
        // Code
    }
}

我无法在 index.js 中初始化主类,因为这会导致以下错误:

I'm unable initialize the main class in index.js as this results in the following error:

module.exports = class Events extends Base {
                                      ^

TypeError: Class extends value undefined is not a constructor or null

我是否以某种方式要求循环授课?我不确定我在这里想念什么.

Am I somehow requiring the classes in a circular way? I'm not sure what I'm missing here.

推荐答案

有可能通过在index.js中导入外部模块的方式来创建某种循环依赖循环.

It is possible you are creating some sort of circular dependency loop by the way you import external modules in index.js.

但是,似乎大多数情况下,您只需要对此进行更改:

But, it appears that mostly you just need to change from this:

const { Base } = require('./base.js');

对此:

const Base = require('./base.js');

从这里:

const { Events } = require('./events.js');

对此:

const Events = require('./events.js');


当您执行以下操作时:


When you do something like this:

const { Base } = require('./base.js');

正在寻找名为 Base 的导入模块上的属性,但是没有这样的属性.您将类导出为整个模块.因此,这就是您需要分配给变量 Base (整个模块)的地方:

it's looking for a property on the imported module named Base, but there is no such property. You exported the class as the whole module. So, that's what you need to assign to your variable Base (the whole module):

const Base = require('./base.js');


如果尚未正确导入index.js中的代码,则导入main.js的代码也必须正确完成.


And the code in index.js that imports main.js will have to be done properly too if it isn't already being done right.

这篇关于如何修复“类扩展未定义的值不是构造函数或null",NodeJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆