简体   繁体   English

为什么节点不识别这个导入的函数?

[英]Why doesn't node recognize this imported function?

I define this function a file like this, and immediately export it. 我将此函数定义为这样的文件,并立即导出它。

const watchMongo = () => {
        console.log("foo")
    };

module.exports = { watchMongo };

Then I import it and run it the main app, as shown here. 然后我导入它并运行主应用程序,如下所示。

const watchMongo = require('./controllers/path');

watchMongo();

However, I get this error when ran. 但是,我在运行时遇到此错误。 "watchMongo is not a function". “watchMongo不是一个功能”。 When I console log 'watchMongo' instead of running it, I'm told "{ watchMongo: [Function: watchMongo] }" 当我控制日志'watchMongo'而不是运行它时,我被告知“{watchMongo:[Function:watchMongo]}”

So Node sees and recognizes the function? 那么Node看到并识别出这个功能? Until the function needs to be ran? 直到功能需要运行? What?? 什么??

You can do one of 2 things. 你可以做两件事之一。 You are defining watchMongo as a named export. 您将watchMongo定义为命名导出。 You can either do: 你可以这样做:

const { watchMongo } = require('./controllers/path');

or on your declaration file export like: 或者在您的申报文件中导出如下:

module.exports = watchMongo;

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

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