我试图在
forEach
循环内调用函数,该循环位于另一个 forEach
循环内,但它不会返回/调用该函数。
我已经看过这个问题,但由于我仍然是新手,我似乎无法使其发挥作用。
这是我的代码(它返回天蓝色中每个服务原则的密码凭据,然后检查到期日期并根据该日期和运行日期的差异进行输出)。如果符合前 2 个
if
条件,我想触发对 JIRA 的 API 调用来创建票证。
const execute = async () => {
allAdApps = await exec('az ad app list --all');
const allAdAppsJson = JSON.parse(allAdApps);
const servicePrinciples = allAdAppsJson.filter((app) => app.displayName);
servicePrinciples.forEach((sp) => {
sp.passwordCredentials.forEach((passwordCred) => {
const formatExpiryDate = new Date(passwordCred.endDateTime);
const diffInTime = formatExpiryDate.getTime() - currentDate.getTime();
const diffInDays = Math.floor(diffInTime / msInOneDay);
if (diffInDays <= 30 && diffInDays > 0) {
const logStr2 = `${sp.displayName}' will expire in '${diffInDays}' days.`;
logger.info(logStr2);
// my function call here
const output = jiraCheckDuplicate('Ticket title');
} else if (diffInDays < 0) {
const expStr = `'${sp.displayName}' already expired '${diffInDays}' days ago.`;
logger.info(expStr);
// my function call here
const output = jiraCheckDuplicate('Ticket title');
} else {
logger.info('string');
}
});
});
};
execute()
.then(() => process.exit())
.catch((err) => {
logger.error(err.message);
process.exit(1);
});
我尝试将
forEach
更改为 for of
,但我的函数仍然没有任何响应
const execute = async () => {
allAdApps = await exec('az ad app list --all');
const allAdAppsJson = JSON.parse(allAdApps);
const servicePrinciples = allAdAppsJson.filter((app) => app.displayName);
for (const sp of servicePrinciples) {
for (const passwordCred of sp.passwordCredentials) {
const formatExpiryDate = new Date(passwordCred.endDateTime);
const diffInTime = formatExpiryDate.getTime() - currentDate.getTime();
const diffInDays = Math.floor(diffInTime / msInOneDay);
if (diffInDays <= 30 && diffInDays > 0) {
const logStr2 = `${sp.displayName}' will expire in '${diffInDays}' days.`;
logger.info(logStr2);
// my function call here
await jiraCheckDuplicate('Ticket title', user, token);
} else if (diffInDays < 0) {
const expStr = `'${sp.displayName}' already expired '${diffInDays}' days ago.`;
logger.info(expStr);
// my function call here
await jiraCheckDuplicate('Ticket title', user, token);
} else {
logger.info('string');
}
}
}
};
execute()
.then(() => process.exit())
.catch((err) => {
logger.error(err.message);
process.exit(1);
});
我尝试过使用
.map
和promises
,但再次无济于事。 (参见这个问题)
const execute = async () => {
allAdApps = await exec('az ad app list --all');
const allAdAppsJson = JSON.parse(allAdApps);
const servicePrinciples = allAdAppsJson.filter((app) => app.displayName);
const promises2 = servicePrinciples.map(async (sp) => {
const promises = sp.passwordCredentials.map(async (passwordCred) => {
const formatExpiryDate = new Date(passwordCred.endDateTime);
const diffInTime = formatExpiryDate.getTime() - currentDate.getTime();
const diffInDays = Math.floor(diffInTime / msInOneDay);
if (diffInDays <= 30 && diffInDays > 0) {
const logStr2 = `${sp.displayName}' will expire in '${diffInDays}' days.`;
logger.info(logStr2);
// my function call here
await jiraCheckDuplicate('Ticket title', user, token).promise();
} else if (diffInDays < 0) {
const expStr = `'${sp.displayName}' already expired '${diffInDays}' days ago.`;
logger.info(expStr);
// my function call here
await jiraCheckDuplicate('Ticket title', user, token).promise();
} else {
logger.info(logStr);
}
});
await Promise.all(promises);
});
};
execute()
.then(() => process.exit())
.catch((err) => {
logger.error(err.message);
process.exit(1);
});
我已将函数简化为“hello world”输出,以消除 cURL 请求中任何可能的错误 - 该函数在
for
循环之外调用时确实可以工作
const jiraCheckDuplicate = async (
summary,
user,
token,
) => {
const auth = `${user}:${token}`;
const urlSummary = encodeURIComponent(summary);
fetch(`https://enterprisevm.atlassian.net/rest/api/2/search?jql=summary~%22'${urlSummary}'%22+and+status!="Done"`, {
method: 'GET',
headers: {
Authorization: `Basic ${Buffer.from(
auth,
).toString('base64')}`,
Accept: 'application/json',
'Content-Type': 'application/json',
},
})
.then((response) => {
logger.info(
`Response: ${response.status} ${response.statusText}`,
);
return response.text();
})
.then((text) => logger.info(text))
.catch((err) => logger.error(err));
};
module.exports = jiraCheckDuplicate;
最终归结为使用
for (const sp of servicePrinciples) {
for (const passwordCred of sp.passwordCredentials) {
删除
process.exit()
并将
jiraCheckDuplicate
更新为具有 return
的异步函数
const jiraCheckDuplicate = async (
我还没有足够的声誉来投票,但感谢@Bergi 和@James 推动我得到这个答案。