为什么我的节点事件回调在事件中显示为未定义?
Why is my node event callback showing up in the event as undefined?
我正在尝试将 url 字符串作为 "data" 传回我的事件处理程序。
我对服务器的请求是“https://172.20.10.6/index.html”。
如我所料,下面的 console.log
打印出“/index.html”。
switch(req.method) {
case 'GET':
console.log("logged from plugin-https.js: url: " + req.url);
ee.emit("dataIn", req.url);
break;
下面是事件处理代码。
console.log(data);
的计算结果为 "undefined"。我想弄清楚这是为什么。这似乎是一个非常简单的直接回调,所以我想知道为什么数据在事件处理代码中出现时没有定义。
plugin = rtrn;
console.log('4');
plugin.dataIn(config, function(err, data) {
if(err, data) {
console.log('error geting data');
} else {
// This is where request events are being handled.
console.log(data);
console.log('We are in the event handling codeblock of funtion startRuntime() located in interactor.js');
此外,这是我的回调代码
var ee = new events.EventEmitter();
function dataIn(config, callback) {
if(typeof callback === 'function') {
ee.on("dataIn", callback);
您似乎在吞下错误。
if(err, data) {
console.log('error geting data');
}
条件 (err, data)
将忽略 err
的值。如果 data
未定义,它将是假的,你不会知道你有一个错误。你可能想要更多这样的东西:
if (err) {
console.log('error getting data')
}
追溯到你的发射器,你正在以一种将数据发送到第一个参数的方式发射,这应该是错误发生的地方。所以这个:
ee.emit("dataIn", req.url);
...应该更像这样:
ee.emit("dataIn", null, req.url);
我正在尝试将 url 字符串作为 "data" 传回我的事件处理程序。
我对服务器的请求是“https://172.20.10.6/index.html”。
如我所料,下面的 console.log
打印出“/index.html”。
switch(req.method) {
case 'GET':
console.log("logged from plugin-https.js: url: " + req.url);
ee.emit("dataIn", req.url);
break;
下面是事件处理代码。
console.log(data);
的计算结果为 "undefined"。我想弄清楚这是为什么。这似乎是一个非常简单的直接回调,所以我想知道为什么数据在事件处理代码中出现时没有定义。
plugin = rtrn;
console.log('4');
plugin.dataIn(config, function(err, data) {
if(err, data) {
console.log('error geting data');
} else {
// This is where request events are being handled.
console.log(data);
console.log('We are in the event handling codeblock of funtion startRuntime() located in interactor.js');
此外,这是我的回调代码
var ee = new events.EventEmitter();
function dataIn(config, callback) {
if(typeof callback === 'function') {
ee.on("dataIn", callback);
您似乎在吞下错误。
if(err, data) {
console.log('error geting data');
}
条件 (err, data)
将忽略 err
的值。如果 data
未定义,它将是假的,你不会知道你有一个错误。你可能想要更多这样的东西:
if (err) {
console.log('error getting data')
}
追溯到你的发射器,你正在以一种将数据发送到第一个参数的方式发射,这应该是错误发生的地方。所以这个:
ee.emit("dataIn", req.url);
...应该更像这样:
ee.emit("dataIn", null, req.url);