From c902875f3ca04e67be287227e8c8b3ac7ea0a717 Mon Sep 17 00:00:00 2001 From: ChengLei Shao Date: Sat, 3 Aug 2024 21:54:37 +0800 Subject: [PATCH] chore(action-export): error message when field not found (#4890) --- .../server/__tests__/export-to-xlsx.test.ts | 42 +++++++++++++++++++ .../src/server/xlsx-exporter.ts | 3 ++ 2 files changed, 45 insertions(+) diff --git a/packages/plugins/@nocobase/plugin-action-export/src/server/__tests__/export-to-xlsx.test.ts b/packages/plugins/@nocobase/plugin-action-export/src/server/__tests__/export-to-xlsx.test.ts index 1068180ff..9887d3576 100644 --- a/packages/plugins/@nocobase/plugin-action-export/src/server/__tests__/export-to-xlsx.test.ts +++ b/packages/plugins/@nocobase/plugin-action-export/src/server/__tests__/export-to-xlsx.test.ts @@ -324,6 +324,48 @@ describe('export to xlsx', () => { await app.destroy(); }); + it('should throw error when export field not exists', async () => { + const Post = app.db.collection({ + name: 'posts', + fields: [ + { + name: 'title', + type: 'string', + }, + ], + }); + + await app.db.sync(); + + await Post.repository.create({ + values: { + title: 'some_title', + json: { + a: { + b: 'c', + }, + }, + }, + }); + + const exporter = new XlsxExporter({ + collectionManager: app.mainDataSource.collectionManager, + collection: Post, + chunkSize: 10, + columns: [{ dataIndex: ['json'], defaultTitle: '' }], + }); + + let error: any; + try { + await exporter.run({}); + } catch (e) { + error = e; + } + + expect(error).toBeDefined(); + expect(error.message).toContain('not found'); + }); + it('should export with json field', async () => { const Post = app.db.collection({ name: 'posts', diff --git a/packages/plugins/@nocobase/plugin-action-export/src/server/xlsx-exporter.ts b/packages/plugins/@nocobase/plugin-action-export/src/server/xlsx-exporter.ts index fd1534394..9b92b05d3 100644 --- a/packages/plugins/@nocobase/plugin-action-export/src/server/xlsx-exporter.ts +++ b/packages/plugins/@nocobase/plugin-action-export/src/server/xlsx-exporter.ts @@ -114,6 +114,9 @@ class XlsxExporter { } const field = this.options.collection.getField(col.dataIndex[0]); + if (!field) { + throw new Error(`Field "${col.dataIndex[0]}" not found: , please check the columns configuration.`); + } if (field.isRelationField()) { return col.dataIndex[0];