类型错误('app.use() requires a middleware function')

TypeError('app.use() requires a middleware function')

enter image description here

这里是app.js(--请找到附件):

//here's the app.js 
const express = require("express");
const app = express();
app.use(express.json());
const morgan = require("morgan");
const mongoose = require("mongoose");
const cors = require("cors");
require("dotenv/config");
const authJwt = require("./helpers/jwt");
const errorHandler = require("./helpers/error-handler");
app.use(cors());
app.options('*', cors());


//middleware
app.use(morgan("tiny"));
app.use(authJwt());
app.use(errorHandler());  //-> **this isn't correct?**


//Importing routing of products
const categoriesRoutes = require("./routes/categories");
const productsRoutes = require("./routes/products");
const usersRoutes = require("./routes/users");
const ordersRoutes = require("./routes/orders");
const req = require("express/lib/request");
const res = require("express/lib/response");



const api = process.env.API_URL;

//routers
app.use(`${api}/products`, productsRoutes);
app.use(`${api}/categories`, categoriesRoutes);
app.use(`${api}/orders`, ordersRoutes);
app.use(`${api}/users`, usersRoutes);

//Database
mongoose
  .connect(process.env.CONNECTION_STRING, {
    useNewurlParser: true,
    useUnifiedTopology: true,
    dbName: "eshop-database",
  })
  .then(() => {
    console.log("database connection is ready");
  })
  .catch((err) => {
    console.log(err);
  });

 
  
//Server
app.listen(3000, () => {
  console.log("Server is Running http://http://localhost:3000");
});
//错误图片

//here's the error handler code
function errorHandler(err, req, res, next){
    if (err) {
        res.status(500).json({message: err})
    }
}
    module.exports = errorHandler;

> Blockquote (--please find attached image)
here's the error 
C:\Users\steve\Backend\node_modules\express\lib\application.js:210
throw new TypeError('app.use() requires a middleware function')
^

TypeError: app.use() requires a middleware function
at Function.use (C:\Users\steve\Backend\node_modules\express\lib\application.js:210:11)
at Object.<anonymous> (C:\Users\steve\Backend\app.js:17:5)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:17:47

您的错误处理函数是正确的,但问题是您是在调用该函数,而不是将其直接传递给您的 Express 应用。

删除 errorHandler 后的括号,它应该可以工作。

app.use(errorHandler);  // Don't call errorHandler, express will call it

这样想。

如果我只是在任何上下文中调用 errorHandler() 它不会 return 任何东西。

这意味着 errorHandler() 的计算结果为 undefined

现在在您的代码中,当您执行 app.use(errorHandler()) 而不是计算为以下内容时:

app.use(function(err, req, res, next){
    if (err) {
        res.status(500).json({message: err})
    }
});

您的代码实际计算为

app.use(undefined);

所以 express 抛出错误,因为 undefined 不是一个函数。