adminjs-nestjs
adminjs-nestjs copied to clipboard
Custom Action is not working properly
I have created a custom action for my user model to export all users in an Excel file.
for this I have created this action
actions: {
export: {
actionType: 'resource',
component: false,
isAccessible: () => true,
handler: async (request, response, context) => {
const users = await userModel.find({})
const workbook = new ExcelJS.Workbook();
const worksheet = workbook.addWorksheet('Users');
worksheet.addRow(['first_name', 'last_name', 'phone', 'last_login', 'name', 'email', 'isActive', 'domain_id', 'createdAt']);
users.forEach(user => {
worksheet.addRow([user.first_name, user.last_name, user.phone, user.last_login, user.name, user.email, user.isActive, user.domain_id, user.createdAt ]);
});
response.setHeader('Content-Type', 'text/csv')
response.setHeader('Content-Disposition', 'attachment; filename=users.csv')
await workbook.csv.write(response);
response.end();
},
guard: 'This will export all users',
icon: 'CopyIcon',
label: 'Export Users',
},
}
now when I click on the export button it hits the API in the background with 200 status and shows csv format in API response but I don't start downloading or open API response in a new tab or anything that helps the user to download Excel.
I am using exceljs library for exporting users. I have tried all different methods but nothing helps I don't want to create a custom component. if anyone have idea how can I fix this?