for api request I go trough some arrays:
'mail', '20.03.2017 05:32', '11.07.2017 03:44', '2', '0', '2', '0', '4', '3', '46' ]
for each array I do 3 nested request But now I have the problem that my index for the requests is sometimes wrong. I think that my loop is faster than the request. How can I take the following way:
- Start for loop index = 1
- make api call 1 with index 1
- wait for api call 1 response
- make api call 2 with index 1
- wait for response of api call 2
- make api call 3 with index 1
wait for response of api call 3
index++
make api call 1 with index 2 ......
for (var i = 1; i <= (csvData.length-1); i++){
var options = { method: 'GET',
url: 'https://api.pipedrive.com/v1/persons/find',
qs:
{ term: csvData[i][0],
start: '0',
search_by_email: '1',
api_token: '' },
headers:
{ 'postman-token': '',
'cache-control': 'no-cache' } };
request(options, function (error, response, body) {
if (error) throw new Error(error);
var user = JSON.parse(body);
console.log("-->User gefunden<--");
console.log("-->User: "+user.data[0].name+"<--");
var options = { method: 'GET',
url: 'https://api.pipedrive.com/v1/deals/find',
qs:
{ term: user.data[0].name,
api_token: '' },
headers:
{ 'postman-token': '',
'cache-control': 'no-cache' } };
request(options, function (error, response, body) {
if (error) throw new Error(error);
var deal = JSON.parse(body);
var options = { method: 'PUT',
url: 'https://api.pipedrive.com/v1/deals/'+deal.data[0].id,
qs: { api_token: '' },
headers:
{ 'postman-token': '',
'cache-control': 'no-cache',
'content-type': 'application/json' },
body: { stage_id: csvData[i-1][9]},
json: true };
request(options, function (error, response, body) {
if (error) throw new Error(error);
});
});
});
}
});