Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DocumentService Lifecycles/Middlewares cannot be registered #19998

Closed
derrickmehaffy opened this issue Apr 2, 2024 · 5 comments
Closed

DocumentService Lifecycles/Middlewares cannot be registered #19998

derrickmehaffy opened this issue Apr 2, 2024 · 5 comments
Labels
issue: bug Issue reporting a bug severity: medium If it breaks the basic use of the product but can be worked around source: core:core Source is core/core status: confirmed Confirmed by a Strapi Team member or multiple community members version: 5

Comments

@derrickmehaffy
Copy link
Member

Bug report

Required System information

  • Node.js version: v20.11.1
  • Yarn version: 1.22.22
  • Strapi version: 5.0.0-beta.1
  • Database: SQLite
  • Operating system: Linux Mint 21.3
  • Is your project Javascript or Typescript: Javascript

Describe the bug

When trying to register a new documentService lifecycle around any of the functions an error is thrown that breaks the content-manager: [2024-04-02 11:53:59.023] error: middlewares[(index++)] is not a function

Steps to reproduce the behavior

  1. Create a new Strapi 5 beta project
  2. Try to register a new documentService Lifecycle in the ./src/index.js (See code example below)
  3. Start the application and try and go to the content-manager
  4. See error

Expected behavior

Should register the lifecycle

Screenshots

image

[2024-04-02 12:00:42.831] error: middlewares[(index++)] is not a function
TypeError: middlewares[(index++)] is not a function
    at next (/mnt/storage/Development/testLifecyclesV5/node_modules/@strapi/core/dist/services/document-service/middlewares/middleware-manager.js:14:38)
    at Array.databaseErrorsMiddleware (/mnt/storage/Development/testLifecyclesV5/node_modules/@strapi/core/dist/services/document-service/middlewares/errors.js:13:18)
    at next (/mnt/storage/Development/testLifecyclesV5/node_modules/@strapi/core/dist/services/document-service/middlewares/middleware-manager.js:14:38)
    at Object.run (/mnt/storage/Development/testLifecyclesV5/node_modules/@strapi/core/dist/services/document-service/middlewares/middleware-manager.js:18:14)
    at Object.newMethod [as findMany] (/mnt/storage/Development/testLifecyclesV5/node_modules/@strapi/core/dist/services/document-service/middlewares/middleware-manager.js:32:30)
    at Object.findPage (/mnt/storage/Development/testLifecyclesV5/node_modules/@strapi/plugin-content-manager/server/src/services/document-manager.ts:86:31)
    at Object.find (/mnt/storage/Development/testLifecyclesV5/node_modules/@strapi/plugin-content-manager/server/src/controllers/collection-types.ts:135:70)
    at runNextTicks (node:internal/process/task_queues:60:5)
    at process.processImmediate (node:internal/timers:449:9)
    at process.topLevelDomainCallback (node:domain:160:15)
[2024-04-02 12:00:42.832] http: GET /content-manager/collection-types/api::test.test?page=1&pageSize=10&sort=test:ASC (21 ms)

Code snippets

// path: ./src/index.js

"use strict";

module.exports = {
  register(/*{ strapi }*/) {},

  bootstrap(/*{ strapi }*/) {
    strapi.documents.use("findMany", async (ctx, next) => {
      console.log("test");
    });
  },
};

Additional context

Related to: strapi/documentation#2091

@derrickmehaffy derrickmehaffy added issue: bug Issue reporting a bug severity: medium If it breaks the basic use of the product but can be worked around status: confirmed Confirmed by a Strapi Team member or multiple community members version: 5 source: core:core Source is core/core labels Apr 2, 2024
@alexandrebodin
Copy link
Member

alexandrebodin commented Apr 3, 2024

Hi, this is an invlid issue,

The document service middlewares have to be used as follows:

strapi.documents.use((ctx, next) => {
	
	// ctx.action gives the names of the action
	// ctx.uid gives the name of the content-type uid
	// ctx.args is the array of arguments received by the called action
	
	return next();
})

@laurenskling
Copy link
Contributor

Hi, this is an invlid issue,

The document service middlewares have to be used as follows:

strapi.documents.use((ctx, next) => {
	
	// ctx.action gives the names of the action
	// ctx.uid gives the name of the content-type uid
	// ctx.args is the array of arguments received by the called action
	
	return next();
})

testing with this and it's actually ctx.contentType with the entire contentType object, not ctx.uid

@laurenskling
Copy link
Contributor

Wouldn't it make sense to do this?:

strapi.documents('api::my-content-type').use((ctx, next) => {
  return next();
})

@derrickmehaffy
Copy link
Member Author

I rewrote one of my old examples that switched from model lifecycles to the docsrv middlewares: https://github.com/strapi-support-demo-apps/strapi-example-v5-injectUUID

There are a few other issues/improvements I noticed but so far it's not too terrible to work with.

I'm with you that it would be nice to only specifically inject a middleware for a specific uid or have a way to provide a list of uids to pre-filter (the dynamic model lifecycles using subscribe has that).

The other thing I'm not a fan of is the structure of the args: [] since it's an array with either just an object or a string (documentId) + the object. Makes it tricky to work with easily.

@alexandrebodin
Copy link
Member

work in progress in here #20017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
issue: bug Issue reporting a bug severity: medium If it breaks the basic use of the product but can be worked around source: core:core Source is core/core status: confirmed Confirmed by a Strapi Team member or multiple community members version: 5
Projects
Archived in project
Status: Done
Development

No branches or pull requests

3 participants