我有一个runningService.js可以做到这一点:
child.stdout.on('data', function(uuid, data) {
getEventLog.getId(uuid);
});
执行时我收到以下错误:
/getEventLog.js:35 callback(null,arrFound); ^
TypeError:回调不是函数 在IncomingMessage。 (/Users/paulcarron/git/integration-test-runner/modules/getEventLog.js:35:13) 在emitNone(events.js:111:20) 在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)
我的回调有什么问题?
这是getEventLog.js:
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 (uuid, 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(uuid) > -1) {
return body[key].name;
}
}).reduce(function(obj, key){
obj = body[key].id;
return obj;
}, {});;
callback(null, arrFound);
});
});
req.end();
}
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();
})
};
更新
我创建了一个名为test.js的新文件。这是内容:
var getEventLog = require('./getEventLog');
var fs = require('fs');
var readline = require('readline')
const uuidv1 = require('uuid/v1');
getEventLog.getId("251cf1e0-019c-11e8-935d-271b24e13f18", function(err, id){
if(err) return console.log(err)
console.log("ID: " + id)
//console.log("Duration: " + id)
});
console.log("ID Var: " + myId)
getEventLog.getDuration(myId, function(err, duration){
if(err) return console.log(err)
console.log("Duration: " + duration)
});
getEventLog.getDuration("app-20180125065122-0007", function(err, duration){
if(err) return console.log(err)
console.log("Duration: " + duration)
});
我为了这个测试而硬编码了uuid(251cf1e0-019c-11e8-935d-271b24e13f18
)。当我运行test.sh时,我得到以下输出:
ID Var: undefined
ID: app-20180125065122-0007
modules/getEventLog.js:35
callback(null, arrFound);
^
TypeError: callback is not a function
at IncomingMessage.<anonymous> (modules/getEventLog.js:35: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)
第一件事是var myId显示为未定义。我想我需要在getEventLog.getId中使用类似回调的东西,但我不确定如何构造它。
第二,即使我将myId的预期值硬编码到getEventLog.getDuration中,我也会收到回调错误。我猜这也有问题,但是我完全不知道这个。
更新2
我从getEventLog.exports.getDuration中删除了getEventLog.getId(function(err, id){
和关联的})
。现在当我运行以下代码时,我得到ID(app-20180125065122-0007)和持续时间。我仍在硬编码id,所以我现在需要弄清楚如何将它分配给test.sh中第一个函数的变量。
答案 0 :(得分:2)
获取id获取id和函数,但只发送id
getEventLog.getId(uuid);//change to
getEventLog.getId(uuid,function(err,arrfound) =>{
//do somthing
});