Javascript 多个REST API调用连续返回未定义的
我试图使用Javascript 多个REST API调用连续返回未定义的,javascript,node.js,jira-rest-api,Javascript,Node.js,Jira Rest Api,我试图使用JIRA连接器包查询一些JIRA问题。 我遇到了一些问题,返回的数据在代码中执行完所有其他操作后才被定义。我不确定这是否是并发性的问题,但我一辈子都搞不清楚我在哪里,怎么搞砸了 如果在getjiratimemestimations函数中,只要它工作正常并且我能够访问数据以便在程序中进一步使用,就只调用getjiratimestimate。当我试图在map或foreach中执行此操作时,我在Array.from(dataFromMaconomy.keys())数组中迭代,似乎遇到了问题
JIRA连接器
包查询一些JIRA问题。
我遇到了一些问题,返回的数据在代码中执行完所有其他操作后才被定义。我不确定这是否是并发性的问题,但我一辈子都搞不清楚我在哪里,怎么搞砸了
如果在getjiratimemestimations
函数中,只要它工作正常并且我能够访问数据以便在程序中进一步使用,就只调用getjiratimestimate
。当我试图在map
或foreach
中执行此操作时,我在Array.from(dataFromMaconomy.keys())
数组中迭代,似乎遇到了问题
我的理解是,在getjiratimemestimate
函数中添加.then().catch()
应该足以阻止它在所有调用完成之前继续运行?还是我误解了异步调用在Node和JS中的工作方式
我还尝试将其转换为一个异步getjiratimememestimations
,并在搜索前面添加一个wait
。但它似乎也不起作用
在调试过程中,我没有填充dataFromMaconomy
数组。这就是我试图对log语句所做的。log语句现在只打印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()
函数不会返回任何内容。尝试:
function getJiraTimeEstimate(taskNumber, jiraClient) {
return jiraClient.search.search({
jql: `id = ${taskNumber}`,
}).then((res) => res.issues[0].fields.timeoriginalestimate).catch((err) => err);
}
另外,您提到尝试异步/等待,但运气不佳。它的异步版本将是:
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;
}
}
我不确定什么不起作用。您是否希望
generateData()
中的dataWithJira
具有数据(由于从未对数组进行过POUPLATED,因此没有数据)?还是问题出在其他方面?@PawelPsztyc抱歉,我在调试时没有填充数据。这就是我试图对log语句所做的。在上述情况下,log语句只打印undefined
。但是,如果我只在rks数组中的单个项目上调用它,那么它就可以正常工作。谢谢你,我很不好意思忘记从getJiraTimeEstimate()
返回它。但是现在我得到了一系列的承诺。在generateData()。这给了我一系列的承诺。我必须对返回的数据数组执行wait Promise.all()
。但非常感谢,它现在开始工作了@Gurkmeja101这发生在每个人身上,所以不要打败你自己。是的,这会产生一系列你必须解决的承诺。这就是它的工作原理。