首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >导出nodejs中的变量时出错

导出nodejs中的变量时出错
EN

Stack Overflow用户
提问于 2018-09-01 15:00:54
回答 3查看 841关注 0票数 0

我想导出一个变量。但这是会发生的

1文件

代码语言:javascript
复制
const commando = require('discord.js-commando');
const ytdl = require('ytdl-core');


class testCommand extends commando.Command {
    constructor(client) {
        super(client,{
            name: 'test',
            group: 'music',
            memberName: 'test',
            description: 'test',
        });

    }

    async run(message, args) {

        var Testo = 'hello'

    }

}

module.exports.Testo = Testo;
module.exports = testCommand;

2文件

代码语言:javascript
复制
const commando = require('discord.js-commando');
const ytdl = require('ytdl-core');
var Testotest = require('./test.js')


class pauseCommand extends commando.Command {
    constructor(client) {
        super(client,{
            name: 'pause',
            group: 'music',
            memberName: 'pause',
            description: 'Pause music',
        });

    }


    async run(message, args) {

        message.channel.send(Testotest.Testo())

    }

}

module.exports = pauseCommand;

误差

代码语言:javascript
复制
ReferenceError: Testo is not defined
    at Object.<anonymous> (/Users/andrew/Desktop/NullBot_/commands/music/test.js:27:24)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/Users/andrew/Desktop/NullBot_/commands/music/pause.js:3:17)
    at Module._compile (module.js:652:30)

为什么会出错呢?

EN

回答 3

Stack Overflow用户

发布于 2018-09-03 10:06:50

如果运行方法Testo run Testo = 'hello',则在方法run中定义undefined,但是定义class testCommand,因此Testoundefined,只需运行一次方法run即可定义Testo

这段代码

代码语言:javascript
复制
module.exports.Testo = Testo;

设置module.exports = {Testo: Testo}

但你用

代码语言:javascript
复制
module.exports = testCommand;

设置module.exports = testCommand

当您调用Testotest.Testo时是testCommand.Testo (未定义)

在第一个文件中更改代码:

代码语言:javascript
复制
module.exports = testCommand;
module.exports.Testo = Testo;
票数 1
EN

Stack Overflow用户

发布于 2018-09-01 15:08:18

你可能想用这种方式使用module.exports

代码语言:javascript
复制
module.exports = {
    key1: val1,
    key2: val2
}

因此,您的代码module.exports.Testo = Testo; module.exports = testCommand;可以使用这种格式,并且不会抛出错误。

票数 0
EN

Stack Overflow用户

发布于 2018-09-01 16:35:02

您已经定义了这个文件,我的理解是test.js:

代码语言:javascript
复制
const commando = require('discord.js-commando');
const ytdl = require('ytdl-core');


class testCommand extends commando.Command {
    constructor(client) {
        super(client,{
            name: 'test',
            group: 'music',
            memberName: 'test',
            description: 'test',
        });

    }

    async run(message, args) {
      var Testo = 'hello'
    }
}

// Testo is not defined because is under method run
module.exports.Testo = Testo;
module.exports = testCommand;

您现在可以更好地了解问题了,因为问题已经被很好地缩进了。此模块正在加载同步,您将按照同步方式导出Testo,因此,预期会出现错误。如果您想要修复这个问题,您需要在"run“方法之外定义"var Testo”,或者使这个模块异步。

问候

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52129338

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档