如何修复“类扩展值未定义不是构造函数或 null”NodeJS

2024-04-11

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

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

我已经研究了这些答案,但我的问题似乎与以下人员描述的不同。类型错误:类扩展值未定义不是函数或 null https://stackoverflow.com/questions/43176006/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
    }
}

事件.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因为这会导致以下错误:

module.exports = class Events extends Base {
                                      ^

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

我是否以某种方式要求以循环方式上课?我不确定我在这里缺少什么。


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

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

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

to this:

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

从此:

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

to this:

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

当你做这样的事情时:

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

它正在寻找名为的导入模块的属性Base,但不存在这样的属性。您将该类作为整个模块导出。所以,这就是您需要分配给变量的内容Base(整个模块):

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

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何修复“类扩展值未定义不是构造函数或 null”NodeJS 的相关文章

随机推荐