首页 > 解决方案 > Strapi / Nuxt - 找不到自定义路线

问题描述

我用它在strapi和nuxt中设置auth: Auth with Strapi and Nuxt

我目前正在尝试检索特定于经过身份验证的用户的项目(已经检查了这个strapi - 限制用户只获取与他相关的数据)。为此,我在 Strapi (/api/routine/config/routes.json) 中创建了一个自定义路由:

{
  "method": "GET",
  "path": "/routines/me",
  "handler": "Routine.me",
  "config": {
    "policies": []
  }
}

和一个自定义控制器(/api/controllers/Routine.js):

module.exports = {
  me: async (ctx) => {
    const user = ctx.state.user;
    if (!user) {
      return ctx.badRequest(null, [{ messages: [{ id: 'No authorization header was found' }] }]);
    }

    const data = await strapi.services.routine.find({user:user.id});  

    if(!data){
      return ctx.notFound();
    }

    ctx.send(data);
  },
};

我已经通过 Strapi 管理员允许经过身份验证的用户访问“我”。当我从 Nuxt 到达端点时:

const 例程 = 等待 axios.get( http://localhost:1337/routines/me)

我收到此错误:

GET http://localhost:1337/routines/me 404(未找到)

为什么找不到自定义路由?我使用了错误的端点吗?

标签: authenticationroutesnuxt.jsstrapi

解决方案


也许您已经解决了它,但是您似乎忘记了将身份验证标头与请求一起发送。

    const routines = await axios.get(
        'http://localhost:1337/routines/me', {
            headers: {
                Authorization:
                this.$auth.getToken('local'),
            },
        }

推荐阅读