Node js - 因为没有回调函数而返回错误
Posted
技术标签:
【中文标题】Node js - 因为没有回调函数而返回错误【英文标题】:Node js - error returned because no callback function 【发布时间】:2018-06-30 12:08:26 【问题描述】:我有以下节点 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 (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();
);
当我运行它时,会抛出以下错误:
$ 节点 getEventLog.js 815963 /modules/getEventLog.js:73 回调(空,arrFound); ^
ReferenceError: 未定义回调 在传入消息。 (/modules/getEventLog.js:73:13) 在 emitNone (events.js:111:20) 在 IncomingMessage.emit (events.js:208:7) 在 endReadableNT (_stream_readable.js:1056:12) 在 _combinedTickCallback (内部/进程/next_tick.js:138:11) 在 process._tickCallback (internal/process/next_tick.js:180:9)
我觉得我需要创建一个函数,所以我尝试了:
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();
)
;
但它似乎不起作用。我哪里错了?
【问题讨论】:
【参考方案1】:你的第二个例子应该可以工作,你只需要调用它
exports.getDuration(function(err, duration)
if(err) return console.log(err)
console.log(duration)
您的第一个示例不起作用,因为您正在尝试回调数据但尚未指定回调。如果您不想像第二个示例那样创建另一个函数,只需删除回调并使用持续时间数据做任何您需要的事情
//console.log(arrFound);
callback(null, arrFound); //remove this
//arrFound should be the duration, so do whatever you need with it here
【讨论】:
以上是关于Node js - 因为没有回调函数而返回错误的主要内容,如果未能解决你的问题,请参考以下文章