0

I have the below node js script that returns a the duration of a job:

var http = require("http");
var fs = require('fs');
var async = require('async');
var readline = require('readline')
//var db = require('./dbPool');

//get file name
var options =  {
    "method" : "GET",
    "hostname" : "127.0.0.1",
    "port" : "18080",
    "path" : "/api/v1/applications/"
};

exports.getId = function (callback) {
    var req = http.request(options, function (res) {

        var chunks = [];

        res.on("data", function (chunk) {
            chunks.push(chunk);
        });

        res.on("end", function () {
            var body = JSON.parse(Buffer.concat(chunks));

            var arrFound = Object.keys(body).filter(function(key) {
                if (body[key].name.indexOf("TeraGen (5MB)") > -1) {
                    return body[key].name;
                }
            }).reduce(function(obj, key){
                obj = body[key].id;
                return obj;
            }, {});;
            callback(null, arrFound);
        });
    });
    req.end();
}

exports.getId(function(err, id){

    //get file name
    var options =  {
        "method" : "GET",
        "hostname" : "127.0.0.1",
        "port" : "18080",
        "path" : "/api/v1/applications/" + id
    };

    var req = http.request(options, function (res) {

        var chunks = [];

        res.on("data", function (chunk) {
            chunks.push(chunk);
        });

        res.on("end", function () {
            var body = JSON.parse(Buffer.concat(chunks));

            var attempts = body.attempts

            var arrFound = Object.keys(body).filter(function(key) {

                    return attempts[0].duration;

            }).reduce(function(obj, key){
                obj = body.attempts[0].duration;
                return obj;
            }, {});
            //console.log(arrFound);
            callback(null, arrFound);
        });
    });
    req.end();
});

When I run it the following error is thrown:

$ node getEventLog.js 815963 /modules/getEventLog.js:73 callback(null, arrFound); ^

ReferenceError: callback is not defined at IncomingMessage. (/modules/getEventLog.js:73:13) at emitNone (events.js:111:20) at IncomingMessage.emit (events.js:208:7) at endReadableNT (_stream_readable.js:1056:12) at _combinedTickCallback (internal/process/next_tick.js:138:11) at process._tickCallback (internal/process/next_tick.js:180:9)

I have a feeling I need to create a function so I tried:

exports.getDuration = function (callback) {
    exports.getId(function(err, id){

    //get file name
    var options =  {
        "method" : "GET",
        "hostname" : "127.0.0.1",
        "port" : "18080",
        "path" : "/api/v1/applications/" + id
    };

    var req = http.request(options, function (res) {

        var chunks = [];

        res.on("data", function (chunk) {
            chunks.push(chunk);
        });

        res.on("end", function () {
            var body = JSON.parse(Buffer.concat(chunks));

            var attempts = body.attempts

            var arrFound = Object.keys(body).filter(function(key) {

                    return attempts[0].duration;

            }).reduce(function(obj, key){
                obj = body.attempts[0].duration;
                return obj;
            }, {});
            console.log(arrFound);
            callback(null, arrFound);
        });
    });
    req.end();
    })
};

It doesn't seem to work though. Where am I going wrong?

0

1 Answer 1

1

Your second example should work, you just have to call it

exports.getDuration(function(err, duration){
      if(err) return console.log(err)
      console.log(duration)
}

Your first example doesn't work because you are trying to callback the data but you have not specified the callback. If you dont want to create another function like in your second example, just remove the callback and do whatever you need with the duration data

 //console.log(arrFound);
 callback(null, arrFound); //remove this

 //arrFound should be the duration, so do whatever you need with it here
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.