hello i don't understand what is going wrong in my code the array trips line 32 is not fill when i do v.comment = JSON.parse(body_detail).comment at the line 49 and i try to on.emit it after. I did trips[i].comment too but without success. my log says it because i do the on.emit() outside the function. If you have some advice i'll be glad to ear it. thx.
var app = require('express')();
var http = require('http').Server(app);
var https = require("https");
var io = require('socket.io')(http);
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
io.on('connection', (socket) => {
socket.on('push', (msg) => {
var options = {
"method": "GET",
"hostname": "public-api.blablacar.com",
"port": null,
"path": "/api/v2/trips?"+
"fn="+msg.fn+
"&tn="+msg.tn+
"&db="+msg.db+
"&limit=10"+
"&locale=fr_FR"+
"&_format=json",
"headers": {
"accept": "application/json",
"key": "xxx"
}
};
var q = https.request(options, (res) => {
var chunks = [];
res.on("data", (chunk) => { chunks.push(chunk) });
res.on("end", () => {
var body = Buffer.concat(chunks);
var trips = JSON.parse(body).trips;
trips.forEach((v,i) => {
var options = {
"method": "GET",
"hostname": "public-api.blablacar.com",
"port": null,
"path": "/api/v2/trips/"+v.permanent_id,
"headers": {
"accept": "application/json",
"key": "xxx"
}
};
var q = https.request(options, (res) => {
var chunks_detail = [];
res.on("data", (chunk) => { chunks_detail.push(chunk) });
res.on("end", () => {
var body_detail = Buffer.concat(chunks_detail);
v.comment = JSON.parse(body_detail).comment
});
});
q.end();
}, this);
io.emit('pull', trips);
});
});
q.end();
});
});
http.listen(3000, () => { console.log('listening on *:3000') });
//console.log(body.toString());