简体   繁体   English

“错误:Route.get() 需要回调 function 但得到 [object Undefined]” 进行多次导出时

[英]“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.

相关问题 节点:Route.get() 需要一个回调函数,但得到了一个 [object Undefined] - Node: Route.get() requires a callback function but got a [object Undefined] 错误:Route.get()需要回调函数,但得到了一个[object Undefined] - Error: Route.get() requires callback functions but got a [object Undefined] 错误:Route.get()需要回调函数,但得到了一个[object Undefined] NODE.JS + SQL - Error: Route.get() requires a callback function but got a [object Undefined] NODE.JS + SQL 错误:Route.get() 需要回调 function 但在使用导入的 function 时得到 [object Undefined] - Error: Route.get() requires a callback function but got a [object Undefined] while using imported function 错误连接 MongoDB:错误:Route.get() 需要一个回调函数,但得到一个 [object Undefined] - Error connection MongoDB: Error: Route.get() requires a callback function but got a [object Undefined] 错误:Route.get() 需要一个回调函数,但在 app.js 中得到一个 [object Undefined] - Error: Route.get() requires a callback function but got a [object Undefined] at app.js 节点服务器错误:Route.get() 需要回调 function 但得到了 [object Undefined] - node server Error: Route.get() requires a callback function but got a [object Undefined] Express 应用程序的 Route.get() 需要回调函数,但出现 [object Undefined] 错误 - Route.get() for express app requires a callback function but got a [object Undefined] error 错误:Route.get() 需要回调 function 但在 Node.js 中得到了一个 [object Undefined] - Error: Route.get() requires a callback function but got a [object Undefined] in Node.js Route.get() 需要回调函数但是得到了一个 [object Promise] - Route.get() requires callback function but got a [object Promise]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM