D
D
de1m2016-11-07 12:45:44
Node.js
de1m, 2016-11-07 12:45:44

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);
    });
}

The problem is that the "response" is not complete, I think that the connection "req.end();" closes faster than all the data arrives. If for example there is not enough data, then everything works fine.
Can I somehow wait until everything comes and then just call "req.end();"?
I tried to do
res.on('end', function(){
          req.end();
        })

but "req.end();" it all starts at the same time. Who can tell me how to do it right?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
de1m, 2016-11-07
@de1m

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));

Options and the upper part as a whole, I did not add.

S
Severus256, 2016-11-07
@severus256

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.

V
Vitaly, 2016-11-07
@vshvydky

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 question

Ask a Question

731 491 924 answers to any question