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?