strapi / strapi

🚀 Strapi is the leading open-source headless CMS. It’s 100% JavaScript/TypeScript, fully customizable, and developer-first.
https://strapi.io
Other
64.06k stars 8.16k forks source link

Policy not found error #21794

Closed JKtechhw closed 1 month ago

JKtechhw commented 1 month ago

Bug report

Required System information

Describe the bug

Hi, I'm trying to create a custom policy for an API. I edited the core router file for an API named class (which is a collection type created by the Content Type Builder) with the code below. However, when I try to run Strapi again, I get the following error: Error: Error creating endpoint GET /classes: Policy not found.

I attempted to create the policy for the API using the npm strapi generate command and in the router I replaced the function with the policy name, but the same issue persists. The only difference is that the policy name appears in the error message.

Thanks

Steps to reproduce the behavior

  1. Add policy into API core router

Expected behavior

Working policy

Code snippets

'use strict';

/**
 * class router
 */
const { createCoreRouter } = require('@strapi/strapi').factories;

module.exports = createCoreRouter('api::class.class', {
    prefix: '',
    only: ['find', 'findOne'],
    except: [],
    config: {
        find: {
            auth: false,
            policies: [
                (policyContext, config, { strapi }) => {
                    console.log("Policy test")
                }
            ],
            middlewares: [],
        },
        findOne: {},
        create: {},
        update: {},
        delete: {},
    },
});

Additional context

Error detail

Error: Error creating endpoint GET /classes: Policy not found. at resolveHandler (/home/user/Documents//{project name}/strapi/node_ modules/@strapi/core/dist/registries/policies.js:37:13) at /home/user/Documents/{project name}/strapi/node_modules/@strapi/core/dist/registries/policies.js:100:20 at Array.map () at Object.resolve (/home/user/Documents/{project name}/strapi/node_modules/@strapi/core/dist/registries/policies.js:98:35) at Module.createPolicicesMiddleware (/home/user/Documents/{project name}/strapi/node_modules/@strapi/core/dist/services/server/policy.js:6:51) at /home/user/Documents/{project name}/strapi/node_modules/@strapi/core/dist/services/server/compose-endpoint.js:63:16 at createRoute (/home/user/Documents/{project name}/strapi/node_modules/@strapi/core/dist/services/server/routing.js:71:5) at /home/user/Documents/{project name}/strapi/node_modules/@strapi/core/dist/services/server/routing.js:80:9 at Array.forEach () at Object.addRoutes (/home/user/Documents/{project name}/strapi/node_modules/@strapi/core/dist/services/server/routing.js:78:21)

JKtechhw commented 1 month ago

I tried it a second time on version 5.1.0, but I followed the guide on the Strapi blog instead of the documentation. I didn’t attempt to call the policy as a function, but calling it by name, api::class-page.check-class, worked.