I'm writing this code, but as new guy in node.js i'm having some problems... Looks like the "for" loop is being executed again before it even complete it's function. I was expecting it to run, then print the "url", then make the https.get request, wait the response, and, only after the response being received for complete and all the code inside the "for" loop being executed, it run again. But actually what's happening is that it's print in the console log two "url", and after that, it print the two responses headers. (suposing that the "for" loop is running twice)
const express = require('express');
const https = require('https');
const server = express();
server.listen(someport);
server.get('/duplicateorder', (request, response) => {
let storeid = request.query.store;
let orderid = request.query.id;
let amountoforders = orderid.length;
for (let actualorder = 0; actualorder < orderid.length; actualorder++) {
let url = `https://api.somedomain.com/v1/${storeid}/orders/${orderid[actualorder]}`;
console.log({link: url});
https.get(url, response2 => {
let data = [];
let header = response2.headers ? response2.headers : 'no response header';
console.log('Status Code:', response.statusCode);
console.log('Response header:', header);
response2.on('data', chunk => {
data.push(chunk);
console.log(chunk)
});
response2.on('end', () => {
console.log(data)
});
});
};
let res = {length: orderid.length};
return response.json(res);
});
i tried adding the
response2.on('end', () => {
console.log(data)
});
but it did not worked as well.