[英]How do I make propper exports in nodeJS
I want to get my variable containing a file path from my routes to my controller.我想让我的变量包含从我的路由到我的控制器的文件路径。 Eg.
例如。 for router:
对于路由器:
exports.file_path = (req, res) => {
var file_path = storage;
return file_path;
}
Eg.例如。 for controller:
对于控制器:
const { file_path } = require('../routes/auth');
This is one of the combinations of things to try to get my STORAGE varibale from routes.js to controller.js but I ended up being more confused than when I started.这是尝试将我的 STORAGE 变量从 routes.js 到 controller.js 的组合之一,但我最终比开始时更加困惑。 Does anyone have any ideas how to do it?
有没有人有任何想法如何做到这一点?
The error I get:我得到的错误:
throw new Error(msg);
^
Error: Route.post() requires a callback function but got a [object Undefined]
I tried some of the solutions alredy but I can't get them to work:我已经尝试了一些解决方案,但我无法让它们工作:
Error: .post() requires callback functions but got a [object Undefined] not working Route.get() requires a callback function but got a [object Undefined]. 错误:.post() 需要回调函数但得到一个 [object Undefined] 不工作Route.get() 需要一个回调函数但得到一个 [object Undefined]。 What did I do wrong?
我做错了什么? ...
...
不要解构你导出的回调函数:
const file_path_cb = require('../routes/auth');
I just solved this error myself, and I will provide the code to show how I fixed it.我刚刚自己解决了这个错误,我将提供代码来展示我是如何修复它的。
To begin, I have 3 files in play here.首先,我在这里播放 3 个文件。
The error was raised from this file, particularly line 5, where I am importing the router.错误是从这个文件中引发的,特别是第 5 行,我在其中导入了路由器。
//index.js
const express = require("express");
const cors = require("cors");
const db = require("./db/connection");
const userRouter = require("./routes/user-router"); //PROBLEMATIC LINE
const app = express();
const apiPort = 3000;
app.use(express.urlencoded({ extended: true }));
app.use(cors());
app.use(express.json());
db.on("error", console.error.bind(console, "MongoDB connection error:"));
app.get("/", (req, res) => {
res.send("Hello World!");
});
app.use("/api", userRouter);
Clearly, the post function is what is throwing the error.显然, post 函数是抛出错误的原因。 However, after reading through this Stack Overflow thread , I learned that the problem has to do with the connections between the files - the imports and exports of functions.
但是,在阅读了这个 Stack Overflow 线程后,我了解到问题与文件之间的连接有关 - 函数的导入和导出。 This led me to look in the control folder.
这让我查看了控制文件夹。
//user-router.js
const express = require("express");
const UserCtrl = require("../control/user-control"); //PROBLEMATIC LINE (2)
const router = express.Router();
router.post("/user", UserCtrl.createUser); //PROBLEMATIC LINE(1)
module.exports = router;
What I found was that the module.exports line was the issue.我发现是 module.exports 行是问题所在。 What I needed to do was wrap the export in curly braces and the problem went away.
我需要做的是将导出用花括号括起来,问题就消失了。
//user-controller.js
const User = require("../models/user-model");
createUser = async (req, res) => {
//...
};
//module.exports = createUser; //PROBLEMATIC LINE
module.exports = { createUser }; //This fixed the problem
I hope that by showing you the entire breakdown of my problem, you are able to solve yours.我希望通过向您展示我的问题的整个细分,您能够解决您的问题。 Also, I hope that by using my limited knowledge to try to answer a question, I can build up a few reputation points, so that I am able to respond to comments and such, LOL.
另外,我希望通过我有限的知识来尝试回答一个问题,我可以建立一些声誉点,以便我能够回复评论等,哈哈。 Let me know if this was able to help you.
如果这对您有帮助,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.