Answer the question
In order to leave comments, you need to log in
How to wait for the end of request?
I have this function:
icingaapi.prototype.getHostFiltered = function (filter, callback) {
var self = this;
var options = {
hostname: self.url,
port: self.port,
path: '/v1/objects/hosts/',
method: 'POST',
rejectUnauthorized: false,
auth: self.user + ":" + self.pass,
headers: {
"Accept": "application/json",
"Content-Type": "applicatoin/json"
}
}
var req = https.request(options, (res) => {
res.on('data', (d) => {
if (res.statusCode == "200") {
console.log("DATA: " + d);
var output = JSON.parse(d);
return callback(null, output.results);
} else {
return callback({
"Statuscode": res.statusCode,
"StatusMessage": res.statusMessage
}, null);
}
});
res.on('end', function(){
console.log("Res end###################");
})
});
req.write(JSON.stringify(filter));
req.end();
req.on('error', (e) => {
return callback(e, null);
});
}
res.on('end', function(){
req.end();
})
Answer the question
In order to leave comments, you need to log in
I solved the problem.
It looks like this now:
var req = https.request(options, function(res){
res.on('data', function(chunk){
resData += chunk;
})
res.on('end', function(){
if (res.statusCode == "200") {
var output = JSON.parse(resData);
return callback(null, output.results);
} else {
return callback({
"Statuscode": res.statusCode,
"StatusMessage": res.statusMessage
}, null);
}
})
});
req.end(JSON.stringify(filter));
You need to somehow simulate the interaction with the server until all the data arrives. You can try http statuses. For example, while the data is being processed, the server sends status 100 at certain intervals. When the data has arrived and the process is completed, you get status 101 and close the connection.
as far as I understand, you display the answer through write (), this function works in asynchronous mode, and accordingly, the execution of the code behind it does not wait for its completion. And end () completes any issue to the user and your data falls out to nowhere.
request.write (fragment [, encoding] [, callback]) from the dock to the node, note the possibility of using a callback.
And also, why not give Mahomm all the data through end?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question