简体   繁体   中英

Strapi: error when I register custom middlewares

I'm creating middlewares in strapi

I have three models, company, profile, people, and I'm registering three middlewares,

For instance, the middleware for company is in:

src/api/company/middlewares/my-middleware, which is:

module.exports = (config, { strapi }) => {
  return (context, next) => {
    console.log("middleware of company”);
  };
};

I register each middleware in./config/middlewares.js as (I'm following strapi v4 documentation) https://docs.strapi.io/developer-docs/latest/setup-deployment-guides/configurations/required/middlewares.html

module.exports = [
  "strapi::errors",
  "strapi::security",
  "strapi::cors",
  "strapi::poweredBy",
  "strapi::logger",
  "strapi::query",
  "strapi::body",
  "strapi::session",
  "strapi::favicon",
  "strapi::public",
  {
    resolve: "./src/api/company/middlewares/my-middleware",
  },
  {
    resolve: "./src/api/people/middlewares/my-middleware",
  },
  {
    resolve: "./src/api/profile/middlewares/my-middleware",
  },
];

No matter where I place these registrations, the outcome is the same

But now, if I call, from postman, http://localhost:1337/api/companies?populate=*, for example, the middleware is executed but I get an error saying that this address was not found,

If I comment out the middlewares registration, the error disappears but the middlewares are not executed

What could I be doing wrong?

Thanks in advance

It seems like you never call the next function.

module.exports = (config, { strapi }) => {
  return (context, next) => {
    console.log("middleware of company”);
    next()
  };
};

Keep in mind that if your middleware is async, you need to await next().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM