简体   繁体   English

Function 在 nodejs 中未定义错误。 我的代码有什么问题?

[英]Function is not defined error in nodejs. What's wrong with my code?

I created two files, app.js and helpers.js and when I tried to call a function from app.js, I got an error, function is not defined.我创建了两个文件,app.js 和 helpers.js,当我尝试从 app.js 调用 function 时,出现错误,function 未定义。

The two files are located in the same folder.这两个文件位于同一个文件夹中。 I think there is a problem the module.exports keyword,我认为 module.exports 关键字有问题,

can anyone help me here?有人能帮我一下吗? Here is the code of the two separate files:这是两个单独文件的代码:

 //app.js const helpers= require('./helpers'); const total= sum(10,20); console.log("total:",total);

 //helpers.js const sum = (a,b)=>{ return a+b; } module.exports={ sum };

And the error i am getting is:我得到的错误是:

const total= sum(10,20);
             ^

ReferenceError: sum is not defined
    at Object.<anonymous> (E:\testing\app.js:5:14)
    at Module._compile (internal/modules/cjs/loader.js:1147:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1167:10)
    at Module.load (internal/modules/cjs/loader.js:996:32)
    at Function.Module._load (internal/modules/cjs/loader.js:896:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47

Try this way:试试这个方法:

   //app.js

const helpers= require('./helpers');


const total= helpers.sum(10,20);
console.log("total:",total);

//helpers.js

const sum = (a,b)=> {
    return a+b;
}

module.exports = sum;

The problem is that you are exporting your function sum in helpers.js within an object:问题是您要在helpers.js中的 helpers.js 中导出 function sum

// helpers.js

// ...

module.exports = {
    sum
};

If you want to access it from app.js you can either use helpers.sum(...) to access it:如果你想从app.js访问它,你可以使用helpers.sum(...)来访问它:

// app.js

const helpers = require('./helpers');


const total = helpers.sum(10, 20);
console.log("total:", total);

... or you can use object deconstruction in the require line: ...或者您可以在require行中使用 object 解构:

// app.js

const { sum } = require('./helpers');

// ...

Update更新

Sure, the answer you posted does work, but from the name helpers.js I assume that you might want to put multiple helper functions into that file.当然,您发布的答案确实有效,但是从名称helpers.js我假设您可能希望将多个帮助函数放入该文件中。 Then, you must use an object export - as mentioned above - in order to export all helper functions.然后,您必须使用 object 导出 - 如上所述 - 才能导出所有辅助函数。

You can try to edit the way to export the sum function to:您可以尝试编辑将sum function 导出到的方式:

module.exports = sum;

That's because when you define:那是因为当你定义:

module.exports = {...}

it means you're exporting an object, not a function.这意味着您正在导出 object,而不是 function。

Usage:用法:

const sum= require('./helpers');

const total= sum(10,20);
console.log("total:",total);

Finally, I found a my answer.最后,我找到了我的答案。

I just needed to replace the helpers keyword with sum keyword in the first line of app.js and I also removed the curly braces around sum in helpers.js as per other's advice.我只需要在app.js的第一行中用sum关键字替换helpers关键字,并且根据其他人的建议,我还删除了helpers.jssum周围的花括号。

The correct code is below正确的代码如下

//app.js

const sum= require('./helpers');
const total= sum(10,20);

console.log("total:",total);

and

//helpers.js

const sum = (a,b)=>{
return a+b;
}
module.exports=sum;

暂无
暂无

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

相关问题 如何使用 nodejs 在 mysql 数据库中插入 npm uniqid 模块。 当我输入代码时,它显示错误“TypeError: uniqid is not a function” - How to insert npm uniqid modules in mysql database using nodejs. when i put on my code it display an error "TypeError: uniqid is not a function" 不了解我的代码FreeCodeCamp(NodeJS)有什么问题 - Don't Understand what's wrong with my Code, FreeCodeCamp (NodeJS) Node.js。 module.export 有问题; 这不是函数错误 - NodeJs. Problem with module.export; it is not a function error 我的nodejs简单代码有什么问题 - What is wrong with my nodejs simple code 节点。 类型错误:https.request 不是函数 - NodeJS. TypeError: https.request is not a function 如何从NodeJS执行Python。 从Python的函数中获取“无效语法” - How to execute Python from NodeJS. Getting “invalid syntax” in my function from python 试图在 NodeJS 中用 mySQL 做一个准备好的陈述。 此代码的哪一部分不起作用? - Trying to make a preparred statement with mySQL in NodeJS. What part of this code isn't working? 需要帮助使用nodeJS查询MongoDB。 在以下代码中获取未定义不是函数 - Need help querying MongoDB with nodeJS. Getting undefined is not a function on the following code 错误1452 MySQL和NodeJS。 数据库为什么不能正确引用我的表? - Error 1452 MySQL and NodeJS. How come the database cant reference my table right? 无法 POST - 我的 POST 请求有什么问题? 节点 - cannot POST - What's wrong with my POST request? Nodejs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM