SoftwareBrothers / adminjs

AdminJS is an admin panel for apps written in node.js
https://adminjs.co
MIT License
8.23k stars 663 forks source link

[Bug]: handler is not working. #1488

Closed steel-hyuk closed 1 year ago

steel-hyuk commented 1 year ago

Contact Details

hi@u-hoo.com

What happened?

The handler does not work when I give the value of the name option of actions.

Bug prevalence

It happens every moment I try to do that action.

AdminJS dependencies version

"adminjs": "^7.0.0",
"@adminjs/express": "^6.0.0",
"@adminjs/nestjs": "^6.0.1",
"@adminjs/typeorm": "^5.0.0",

What browsers do you see the problem on?

Chrome

Relevant log output

[Nest] 28835  - 2023. 04. 28. 오후 4:10:16   ERROR [ExceptionsHandler] Cannot read properties of undefined (reading 'handler')
TypeError: Cannot read properties of undefined (reading 'handler')
    at ApiController.recordAction (file:///Users/eunhyuk/works/admin/octopus-admin/node_modules/adminjs/lib/backend/controllers/api-controller.js:149:55)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async file:///Users/eunhyuk/works/admin/octopus-admin/node_modules/@adminjs/express/lib/buildRouter.js:24:22

Relevant code that's giving you issues

export const UserResource: ResourceWithOptions = {
  resource: User,
  options: {
    sort: {
      sortBy: 'id',
      direction: 'asc',
    },
    listProperties: [],
    filterProperties: [],
    editProperties: [],
    showProperties: [],
    actions: {
      delete: {
        name: '삭제',
        isVisible: true,
        icon: 'Trash2',
        guard: 'confirmDelete',
        component: false,
        variant: 'danger',
        handler: async (request, _response, context) => {
          const { record, resource, currentAdmin, h } = context;
          if (!request.params.recordId || !record) {
            throw new NotFoundError(['You have to pass "recordId" to Delete Action'].join('\n'), 'Action#handler');
          }
          try {
            await record.update({ archived: true, archived_at: Date() });
          } catch (err) {
            if (err instanceof ValidationError) {
              const baseMessage = err.baseError?.message || 'thereWereValidationErrors';
              return {
                record: record.toJSON(currentAdmin),
                notice: {
                  message: baseMessage,
                  type: 'error'
                }
              };
            }
            throw err;
          }
          return {
            record: record.toJSON(currentAdmin),
            redirectUrl: h.resourceUrl({ resourceId: resource._decorated?.id() || resource.id() }),
            notice: {
              message: '성공적으로 삭제되었습니다.',
              type: 'success'
            }
          };
        }
      },
    }
  }
}

The delete action does not work. It works when I clear 'name' option.

dziraf commented 1 year ago

I think you should just use locale for translations and leave name undefined in this case