多个 REST API 连续调用 returns 未定义
Multiple REST API calls in succession returns undefined
我正在尝试使用 jira-connector
包查询一些 JIRA 问题。
我正在 运行 解决返回的数据问题,直到我的代码中的所有其他内容都执行完毕后才定义。我不确定这是否是并发问题,但我这辈子都无法弄清楚我在哪里以及如何搞砸了。
If 在 getJiraTimeEstimations
函数中只调用 getJiraTimeEstimate
一旦它工作正常并且我可以访问数据以在程序中进一步使用。当我尝试在 map
或 foreach
中执行此操作时,我在 Array.from(dataFromMaconomy.keys())
数组上进行迭代,我似乎 运行 遇到了问题。
我的理解是在getJiraTimeEstimate
函数中加入.then().catch()
应该就可以阻止它在所有调用结束之前继续运行?还是我误解了异步调用在 Node 和 JS 中的工作方式?
我也试过将其转换为 async getJiraTimeEstimations
并在搜索前添加 await
。不过好像也没用。
我在调试时没有填充 dataFromMaconomy
数组。这就是我试图用日志语句做的。日志语句现在只打印 undefined
。但是如果我只用 rks
数组中的一个项目调用它那么它就可以正常工作。
function getJiraTimeEstimate(taskNumber, jiraClient) {
jiraClient.search.search({
jql: `id = ${taskNumber}`,
}).then((res) => res.issues[0].fields.timeoriginalestimate).catch((err) => err);
}
function getJiraTimeEstimations(dataFromMaconomy) {
const settings = JSON.parse(fs.readFileSync(path.join(__dirname, 'konfig.json'), 'utf8'));
const privateKeyData = fs.readFileSync(path.join(__dirname, settings.jira.consumerPrivateKeyFile), 'utf8');
const jira = new JiraClient({
host: settings.jira.server,
strictSSL: false, // Error: unable to verify the first certificate
rejectUnauthorized: false,
oauth: {
consumer_key: settings.jira.consumerKey,
private_key: privateKeyData,
token: settings.jira.accessToken,
token_secret: settings.jira.accessTokenSecret,
},
});
console.log('getting time estimations from Jira');
const dataFromMaconomyWithJira = [];
const rks = Array.from(dataFromMaconomy.keys());
rks.map((rk) => console.log(getJiraTimeEstimate(rk, jira)));
return dataFromMaconomyWithJira;
}
function generateData(){
const dataWithJira = getJiraTimeEstimations(convertedData);
// More functions where I use the data from getJiraTimeEstimations
// This gets run before all of the getJiraTimeEstimations have finished getting the data.
}
根据您在评论中的说明,getJiraTimeEstimate()
函数没有 return 任何内容。尝试:
function getJiraTimeEstimate(taskNumber, jiraClient) {
return jiraClient.search.search({
jql: `id = ${taskNumber}`,
}).then((res) => res.issues[0].fields.timeoriginalestimate).catch((err) => err);
}
此外,您提到尝试使用 async / await 但没有成功。它的异步版本是:
async function getJiraTimeEstimate(taskNumber, jiraClient) {
try {
const res = await jiraClient.search.search({
jql: `id = ${taskNumber}`,
});
return res.issues[0].fields.timeoriginalestimate;
} catch (e) {
return e;
}
}
我正在尝试使用 jira-connector
包查询一些 JIRA 问题。
我正在 运行 解决返回的数据问题,直到我的代码中的所有其他内容都执行完毕后才定义。我不确定这是否是并发问题,但我这辈子都无法弄清楚我在哪里以及如何搞砸了。
If 在 getJiraTimeEstimations
函数中只调用 getJiraTimeEstimate
一旦它工作正常并且我可以访问数据以在程序中进一步使用。当我尝试在 map
或 foreach
中执行此操作时,我在 Array.from(dataFromMaconomy.keys())
数组上进行迭代,我似乎 运行 遇到了问题。
我的理解是在getJiraTimeEstimate
函数中加入.then().catch()
应该就可以阻止它在所有调用结束之前继续运行?还是我误解了异步调用在 Node 和 JS 中的工作方式?
我也试过将其转换为 async getJiraTimeEstimations
并在搜索前添加 await
。不过好像也没用。
我在调试时没有填充 dataFromMaconomy
数组。这就是我试图用日志语句做的。日志语句现在只打印 undefined
。但是如果我只用 rks
数组中的一个项目调用它那么它就可以正常工作。
function getJiraTimeEstimate(taskNumber, jiraClient) {
jiraClient.search.search({
jql: `id = ${taskNumber}`,
}).then((res) => res.issues[0].fields.timeoriginalestimate).catch((err) => err);
}
function getJiraTimeEstimations(dataFromMaconomy) {
const settings = JSON.parse(fs.readFileSync(path.join(__dirname, 'konfig.json'), 'utf8'));
const privateKeyData = fs.readFileSync(path.join(__dirname, settings.jira.consumerPrivateKeyFile), 'utf8');
const jira = new JiraClient({
host: settings.jira.server,
strictSSL: false, // Error: unable to verify the first certificate
rejectUnauthorized: false,
oauth: {
consumer_key: settings.jira.consumerKey,
private_key: privateKeyData,
token: settings.jira.accessToken,
token_secret: settings.jira.accessTokenSecret,
},
});
console.log('getting time estimations from Jira');
const dataFromMaconomyWithJira = [];
const rks = Array.from(dataFromMaconomy.keys());
rks.map((rk) => console.log(getJiraTimeEstimate(rk, jira)));
return dataFromMaconomyWithJira;
}
function generateData(){
const dataWithJira = getJiraTimeEstimations(convertedData);
// More functions where I use the data from getJiraTimeEstimations
// This gets run before all of the getJiraTimeEstimations have finished getting the data.
}
根据您在评论中的说明,getJiraTimeEstimate()
函数没有 return 任何内容。尝试:
function getJiraTimeEstimate(taskNumber, jiraClient) {
return jiraClient.search.search({
jql: `id = ${taskNumber}`,
}).then((res) => res.issues[0].fields.timeoriginalestimate).catch((err) => err);
}
此外,您提到尝试使用 async / await 但没有成功。它的异步版本是:
async function getJiraTimeEstimate(taskNumber, jiraClient) {
try {
const res = await jiraClient.search.search({
jql: `id = ${taskNumber}`,
});
return res.issues[0].fields.timeoriginalestimate;
} catch (e) {
return e;
}
}