Skip to content

Enrich many service for plugin #36

@Yvres-Vreucheu

Description

@Yvres-Vreucheu

Hi,

Is there a way to use enrichMany service with strapi-plugin-comments ?
For the moment, I've tried to use enrichMany service in many ways :

1 - Extending strapi-plugin-comments controller

module.exports = (plugin) => {
 
  const originalFindAllInHierarchy =
    plugin.controllers.client.findAllInHierarchy

  plugin.controllers.client.findAllInHierarchy = async (ctx) => {
    const response = await originalFindAllInHierarchy(ctx)
    console.log(response)
    return strapi
      .service('plugin::reactions.enrich')
      .enrichMany('plugin::comments.comment', response)
  }

  return plugin
}

Result : i can't see console.log(response)

2 - Lifecycle in strapi bootstrap

strapi.db.lifecycles.subscribe({
      models: ['plugin::comments.comment'],

      async afterFindMany(event) {
        const { result } = event

        const enriched = await strapi
          .service('plugin::reactions.enrich')
          .enrichMany('plugin::comments.comment', result)

        return enriched
      }
    })

Result : error : Cannot read properties of undefined (reading 'reduce')

3 - Middleware

module.exports = (config, { strapi }) => {
  return async (ctx, next) => {
    await next()

    if (!ctx.request.url.startsWith('/api/comments')) return

    const data = ctx.body

    const enriched = await strapi
      .service('plugin::reactions.enrich')
      .enrichMany('plugin::comments.comment', list)

    ctx.body = enriched
  }
}

Result : error : Cannot read properties of undefined (reading 'reduce')

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions