简体   繁体   English

module.exports 中的 Function “不是函数”? - 节点

[英]Function in module.exports "is not a function"? - nodejs

I'm trying to use badPort function inside another function ( getPort ) in module.exports like so:我正在尝试在 module.exports 中的另一个 function ( getPort ) 中使用badPort module.exports ,如下所示:

DEFAULT_PORT = 80;

module.exports = {
        badPort:
                (c_port, min=80, max=90) => {
                        return isNaN(c_port) || !between(c_port, min, max);
                },
        getPort:
                (c_port, min=80, max=90) => {
                        console.log(this.badPort(c_port));
                        return this.badPort(c_port) ? 80 : c_port;
                },
};     

However, when I use this.badPort(c_port) it throws an exception:但是,当我使用this.badPort(c_port)时,它会抛出异常:

TypeError: this.badPort is not a function

But it's clearly a function as initialised right above it.但它显然是在其上方初始化的 function。

If however I take out the () , this.badPort always returns undefined .但是,如果我取出()this.badPort总是返回undefined

Why is this happening and how would I be able to correctly use the function inside module.exports ?为什么会发生这种情况,我如何才能正确使用 function inside module.exports Is it possible to also declare the "Global variable" DEFAULT_PORT in module.exports this way?是否也可以通过这种方式在module.exports中声明“全局变量”DEFAULT_PORT?

You can do it, by changing this to module.exports :您可以通过将其更改为module.exports来做到this一点:

DEFAULT_PORT = 80;

module.exports = {
        badPort:
                (c_port, min=80, max=90) => {
                        return isNaN(c_port) || !between(c_port, min, max);
                },
        getPort:
                (c_port, min=80, max=90) => {
                        console.log(module.exports.badPort(c_port));
                        return module.exports.badPort(c_port) ? 80 : c_port;
                },
};

And about second question... you would have to redefine your module, to use that variable externally, like this:关于第二个问题......你必须重新定义你的模块,才能在外部使用该变量,如下所示:

module.exports.DEFAULT_PORT = 80;

Then you will have access to it:然后你就可以访问它了:

var mod = require('mymodule');
console.log(mod.DEFAULT_PORT);

I am not aware of any other way.我不知道任何其他方式。

Have you try this to change ":" by "=" like:您是否尝试过将“:”更改为“=”,例如:

DEFAULT_PORT = 80;

module.exports = {
    badPort = (c_port, min=80, max=90) => {
                    return isNaN(c_port) || !between(c_port, min, max);
            },
    getPort = (c_port, min=80, max=90) => {
                    console.log(this.badPort(c_port));
                    return this.badPort(c_port) ? 80 : c_port;
            },
};  

To reference badPort from the other function you can write it this way:要从其他 function 引用badPort ,您可以这样写:

DEFAULT_PORT = 80;

const badPort = (c_port, min=80, max=90) => {
  return isNaN(c_port) || !between(c_port, min, max);
};

const getPort = (c_port, min=80, max=90) => {
  console.log(badPort(c_port));

  return badPort(c_port) ? 80 : c_port;
};

module.exports = {
  badPort,
  getPort
};     

And if you're curious on how to import it correctly, here is an example of a correct import from another js file in the same directory:如果您对如何正确导入它感到好奇,这里有一个从同一目录中的另一个 js 文件正确导入的示例:

const port = require('./port.js');
console.log(port.badPort(1000));

And your default port can be an environment variable using process.env https://nodejs.org/dist/latest-v8.x/docs/api/process.html您的默认端口可以是使用process.env的环境变量https://nodejs.org/dist/latest-v8.x/docs/api/process.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM