Super.find() to override an existing controller failed

System Information
  • Strapi Version: “@strapi/strapi”: “^4.3.2”,
  • Operating System: MacOS 12.5
  • Database: Mysql 8.0
  • Node Version: 14.20.0
  • NPM Version: 8.1.0
  • Yarn Version: 1.22.19

Followed the docs, Strapi Internals: Customizing the Backend, in order to customize existing controllers, I used the following codes copied from the page,

    // ./src/api/order/controllers/order.js
    
    ...
    module.exports = createCoreController('api::order.order', ({strapi}) => ({
      confirmOrder: async (ctx, next) => {
        ctx.body = "ok"
      },
      find: async (ctx, next) => {
        // destructure to get `data` and `meta` which strapi returns by default
        const {data, meta} = await super.find(ctx)
        
        // perform any other custom action
        return {data, meta}
      }
    }));

but this cause my strapi server crashed, logs are:

yarn run v1.22.19
$ strapi develop
[2022-08-02 13:07:02.821] debug: ⛔️ Server wasn't able to start properly.
[2022-08-02 13:07:02.822] error: 'super' keyword unexpected here
/Users/crosszheng/CODES/Strapi/demo/my-project-mysql/src/api/book/controllers/book.js:16
    const {data, meta} = await super.find(ctx)
                               ^^^^^

SyntaxError: 'super' keyword unexpected here
    at compileFunction (<anonymous>)
    at wrapSafe (internal/modules/cjs/loader.js:1001:16)
    at Module._compile (internal/modules/cjs/loader.js:1049:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:12)
    at Module.require (internal/modules/cjs/loader.js:974:19)
    at require (internal/modules/cjs/helpers.js:101:18)
    at importDefault (/Users/crosszheng/CODES/Strapi/demo/my-project-mysql/node_modules/@strapi/strapi/lib/utils/import-default.js:6:17)
    at loadFile (/Users/crosszheng/CODES/Strapi/demo/my-project-mysql/node_modules/@strapi/strapi/lib/core/loaders/apis.js:158:14)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

could not find any threads about this, please help.

super is used in arrow function. This is weird. How does this work?

Hii, @crossz
did you find the solution for this?

What I see is that by using the arrow function we are breaking the scope so we can use the super keyword. We could solve it in the following way:

'use strict';

/**
 *  order controller
 */

const { createCoreController } = require('@strapi/strapi').factories;

module.exports = createCoreController('api::order.order', ({ strapi }) => ({
  async confirmOrder (ctx, next) {
    ctx.body = 'ok';
  },
  async find (ctx, next) {
    // destructure to get `data` and `meta` which strapi returns by default
    const {data, meta} = await super.find(ctx)

    // perform any other custom action
    return {data, meta}
  }
}));
2 Likes

This worked for me, thanks !