简体   繁体   English

数组中每个值的新API调用

[英]New API call for each value in an array

I basically want this kind of object as a result: 我基本上希望得到这样的对象:

[{ id: 5040, requester_id: 1234, requester_name: 'John' }, { id: 5046, requester_id: 999, requester_name: 'Jim' }, { id: 5049, requester_id: 543, requester_name: 'Alex' }] [{id:5040,requester_id:1234,requester_name:'John'},{id:5046,requester_id:999,requester_name:'Jim'},{id:5049,requester_id:543,requester_name:'Alex'}]

This is an object of tickets and tickets have multiple properties. 这是票证的对象,票证具有多个属性。

The API I'm using has a call that receives the ticket. 我使用的API有一个调用来接收票证。 No problems retrieving this. 检索此没有问题。 The information I get from this API call is this: 我从此API调用中获得的信息是:

[{ id: 5040, requester_id: 1234 }, { id: 5046, requester_id: 999 }, { id: 5049 requester_id: 543 }] [{id:5040,requester_id:1234},{id:5046,requester_id:999},{id:5049 requester_id:543}]

Based on each requester_id I need to do another API call to retreive the name of the requester. 根据每个requester_id,我需要执行另一个API调用以检索请求者的名称。 For requester_id 1234, it gives me this: 对于requester_id 1234,它给出了以下信息:

{ requester_id: 1234, name: 'John' } {requester_id:1234,名称:“约翰”)

Each requester requires its own API call to get this information. 每个请求者都需要自己的API调用才能获取此信息。

If Nodejs were an async programming language, I would do something like this: 如果Nodejs是一种异步编程语言,我将执行以下操作:

 var tickets = api({ method: 'get', path: '/tickets' }); for (var i=0; i<tickets.length; i++){ var contact = api({ method: 'get', path: '/contact/'+tickets[i].requester_id); tickets[i].name = contact.name; } 

Obviously, this doesn't work. 显然,这是行不通的。

I have tried using callbacks, promises, different tools to get the API data (my preference now is 'request'). 我尝试使用回调,promise和其他工具来获取API数据(我现在的偏好是“请求”)。 I have literally spent 8 hours and I can't get it to work. 我已经花了8个小时,却无法正常工作。

I need the for loop to wait until the first API call is finished. 我需要for循环才能等待第一个API调用完成。 Then I need each iteration of the loop to wait for the previous one to finish. 然后,我需要循环的每个迭代来等待上一个迭代完成。

My API function returns a Promise like this: 我的API函数返回如下的Promise:

 return new Promise(function(resolve, reject) { request.get(options, function(err, resp, body) { if (err) { reject(err); } else { resolve(JSON.parse(body)); } }); }); 

Out of many different failed attempts, this is the last one: 在许多不同的失败尝试中,这是最后一个:

 var tickets = []; var contacts = []; var insertContactData = function(reqId){ return api({ method: 'get', path: '/contacts/'+reqId }); } var main = function(){ var myTickets = api({ method: 'get', path: '/tickets'); myTickets.then(function(result) { var ticketData = result.results; for (var i=0; i < result.results.length; i++){ var thisTicket = {}; thisTicket.id = ticketData[i].id; tickets[i] = thisTicket; } var contacts = []; for (var i=0; i < result.results.length; i++){ var idExists = false; for (var j=0; j < contacts.length; j++){ if (contacts[j].requesterId == tickets[i].requesterId){ idExists = true; } } if (idExists == false){ var newCont = {}; newCont.requesterId = tickets[i].requesterId; contacts.push(newCont); } } contacts.shift(); for (var i=0; i < contacts.length; i++){ var pos = i; var reqId = contacts[i].requesterId; var contact = insertContactData(reqId); console.log(contact); } }, errHandler).catch(function(error){ console.log('ERROR '+error); }); } 

Please help? 请帮忙?

If you need to wait for an asynchronous request before continuing then you can use async / await 如果需要在继续之前等待异步请求,则可以使用async / await

async function loadTicketInfo() { 
  const tickets = await api({ method: 'get', path: '/tickets' });
  for (let i=0; i<tickets.length; i++){
    var contact = await api({ method: 'get', path: '/contact/'+tickets[i].requester_id);
    tickets[i].name = contact.name;
  }
};

Although, depending on how many tickets you expect to pull, this could be a pretty slow operation. 虽然,这取决于您希望拉出多少张票,但操作可能会很慢。 I suggest you pull all the ticket information in parallel, and then marry the information up at the end 我建议您并行提取所有票证信息,然后在末尾将其汇总

const ticketInfos = tickets.map(t => api({ method: 'get', path: '/contact/' + t.requester_id));
const infos = await Promise.all(ticketInfos);
tickets.forEach(t => {
  const info = infos.find(i => i.requester_id === t.requester_id);
  if (info) {
    t.requester_name = t.name;
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM