Jira创建Issue时,数组填充描述表格返回undefined如何解决
问题原因与解决方法
问题出在你使用的forEach方法上:Array.prototype.forEach()没有返回值,执行后默认返回undefined,所以模板字符串中插入的部分就是undefined,导致表格行无法生成。
解决步骤
- 替换
forEach为map:map方法会遍历数组,返回由每个元素处理后的结果组成的新数组,正好适合生成表格行的字符串集合。 - 用
join('')拼接数组:把map返回的字符串数组拼接成完整的文本,插入到模板字符串中。
修改后的代码如下:
const data = { fields: { project: { key: config.jira.project }, summary: "Migration Tool - Migrating repositories", description: `Migrating repositories\n Validated Bitbucket repositories:\n ||ID||Bitbucket Key||Bitbucket repo slug||Github Organization||Github repo name|| ${input.validated.map(item => { return `|${item.id}|${item.bitbucket.projectKey}|${item.bitbucket.repositoryName}|${item.github.org}|${item.github.repositoryName}|\n` }).join('')} Invalid Bitbucket repositories:\n`, issuetype: { name: "Task" }, customfield_10005: config.jira.epic } }
额外提示
- 注意模板字符串的缩进:如果保留过多前置空格,可能导致Jira无法正确解析表格格式,建议表格行的文本不要带多余缩进。
- 如果
input.validated是空数组,map会返回空数组,join后是空字符串,不会影响表格的表头显示。
内容的提问来源于stack exchange,提问作者Alistair Hardy
相关产品推荐
相关产品推荐

