[英]“Error: Route.get() requires a callback function but got a [object Undefined]” when doing multiple exporting
I am trying to export the middleware function so that other classes can call it.我正在尝试导出中间件 function 以便其他类可以调用它。
I did some google search but did not work for my case.我做了一些谷歌搜索,但对我的情况不起作用。
Here is the code这是代码
auth.js auth.js
isLoggedIn = (req, res, next) => {
next();
}
module.exports.isLoggedIn = isLoggedIn;
module.exports = app => {
};
profile.js profile.js
const isLoggedIn = require('./auth').isLoggedIn;
let profile = [];
getAllProfile = (req, res) => {
res.send(profile);
}
module.exports = (app) => {
app.get('/all-profile',isLoggedIn, getAllProfile);
}
index.js index.js
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
app.use(bodyParser.json());
const port = process.env.PORT || 3000;
const server = app.listen(port, () => {
const addr = server.address();
console.log(`Server listening at ${port}`);
});
let auth = require("./src/auth");
auth(app);
let profile = require("./src/profile");
profile(app);
The error message is错误信息是
\node_modules\express\lib\router\route.js:202
throw new Error(msg);
^
Error: Route.get() requires a callback function but got a [object Undefined]
What am I doing wrong here?我在这里做错了什么?
You are overwriting your module.exports
with the second line here:你在这里用第二行覆盖你的
module.exports
:
module.exports.isLoggedIn = isLoggedIn;
module.exports = app => {
};
So .isLoggedIn
is no longer a property of the new exports object you assigned.因此
.isLoggedIn
不再是您分配的新导出 object 的属性。 You could flip the order:您可以翻转订单:
module.exports = app => {
};
module.exports.isLoggedIn = isLoggedIn;
That way, you define a new module.exports
object first (which happens to be a function object) and then add a property to the new object.这样,您首先定义一个新
module.exports
object(恰好是一个 function 对象),然后向新的 object 添加一个属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.