Error: entityManager.findOneWithCreatorRoles is not a function

System Information
  • Strapi Version: 4.15.5 with Typescript
  • Operating System: Windows 11
  • Database: Postgres
  • Node Version: 18.19.0
  • NPM Version: 10.2.3
  • Yarn Version: 1.22.19

I have categories (left) and menus (right) collection types as shown in the image below.

The categories and menus collection types have this type of relation.

The author role could only CRUD the categories and menu data they created.
But when I tried to sign in using one of the author accounts and then click on the category field, I could see all categories from the database (not only the data created by the current author).

I searched on the internet and found the same issue here Author is reading other Author's items, by the related field.
Then I implemented this solution Author can select relational content that another user created. · Issue #7696 · strapi/strapi · GitHub in Typescript.
When I ran the Strapi and then clicked on the category field on the one menu content (like the image above), the Strapi returned this error error: entityManager.findOneWithCreatorRoles is not a function.

Question: how to fix the error above?

I didn’t know if my approach below was correct or not.
I searched on the internet about the error and didn’t find any.
Then I searched entityManageron the Strapi Github repository and found the entity-manager.ts file https://github.com/strapi/strapi/blob/develop/packages/core/content-manager/server/src/services/entity-manager.ts.
I copied and pasted the file into the same folder as the entity-manager.ts file (src → content-manager → entity-manager.ts)

But the error still appeared.

1 Like

I ran into the exact same issue as you, and it looks like that function has been removed at some point. For my particular use-case I ended up replacing the controller entirely. With the collection names from your example, it would look like this:

plugin.controllers.relations.findAvailable = async (ctx) => {
    const adminUserId = ctx.state.user.id;
    const localeCode = ctx.query.locale || 'en';
    const requestType = ctx.request.url.split("/")[3].split(".")[1];

    let query;

    if (requestType === 'category') {
      query = await strapi.db.query('api::menu.menu').findMany({
        where: {
          locale: localeCode,
          createdBy: adminUserId,
        },
        populate: ['createdBy'],
      });
    } else if (requestType === 'menu') {
      query = await strapi.db.query('api::category.category').findMany({
        where: {
          locale: localeCode,
          createdBy: adminUserId,
        },
        populate: ['createdBy'],
      });
    } else {
      return ctx.badRequest(`Unknown request type: "${requestType}". Add new types at src/extensions/content-manager/strapi-server.js`);
    }

    const result = {"results": query};

    return result;
  };

This will simply return a list of collection items that the user making the request created, where the returned collection depends on which collection the request came from, although splitting the request URL like this is not an ideal solution. The collection names are also hard-coded. Making this work for any pair of collections would require more work.

It’s also not using TypeScript, but I hope it’s a good start, if you’re still stuck on this. I’m not an expert, but feel free to ask if anything about this is unclear.

1 Like

Thank you so much for the reply.

“Replacing something entirely” must be a tiring solution. Imagine if there are a lot of things to replace manually.
But I think I have to try it because currently, that’s the last solution I have.
I will tell you if I found something.

1 Like

I think you can use either a lifecycle hook or do it in a middleware and inject the response only returning items that belong to the user that created them. I personally haven’t tried that approach yet, but now I am curious how it would work.

Checkout this example here from Derrick https://github.com/strapi-support-demo-apps/strapi-example-v4-ctm-route-middleware/blob/main/src/index.js

1 Like

Thank you so much for the reply @Paul_Brats .

I used the code from Github but found an error after running the Strapi.
Here is the error Error: Error creating endpoint POST /collection-types/:model: Middleware global::auto-uid not found.

Hi @PhilippEngmann, I just tried your answer but it’s not working fine.

I copied your answer to the src > extensions > content-manager > strapi-server.js and replaced my plugin.controllers.relations.findAvailable with your code.

I created 2 categories here.

Then I tried to create a menu.
I selected the category dropdown but I didn’t find any categories I have created above.

It is not the exact solution. So you would have to modify it according to your needs. You would also need to create the middleware that you want to use.

1 Like