nodejs错误:回调已被调用 [英] nodejs Error: Callback was already called

查看:1653
本文介绍了nodejs错误:回调已被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍然是nodejs和开发自己的异步函数的新手。根据堆栈跟踪,我正在看,我被告知,以下代码被调用两次。特别是catch回调。



有没有更好的方法来构造这个,所以如果尝试中的多个变量打破它只能回调一次?



尽可能接近我所说的,因为所有的缓冲区读取都是异步完成的,如果有多个错误,他们都会同时调用catch,同时导致我的错误。至少这是我唯一可以想到的,会导致这个错误,但对于我的生活,我不能想到一个办法。

  function fun1(buffer_1,ushort_Type,cb){
cb =(typeof cb ==='function'?cb:function(){});
var jsonData = {};

try {
var uint_val1 = buffer_1.readUInt32LE(4);
var string1_val2 = buffer_1.toString('utf8',12,45);
var ubyte_val3 = buffer_1.readUInt8(46);

jsonData.Type = ushort_Type;
jsonData.val1 = uint_val1;
jsonData.val2 = string1_val2;
jsonData.val3 = ubyte_val3;

cb(null,jsonData);
} catch(err){
cb(err); // ln 393
}
}

错误堆栈跟踪

  FolderWatcher-3 [26/01/2017 17:16:45.898] [错误]错误:回调已被调用。 
FolderWatcher-3在C:\\\
odeCode\FolderWatcher\\\
ode_modules\async\dist\async.js:837:36
FolderWatcher-3在C:\\\
odeCode\FolderWatcher在\\\
odeCode中的\parse.js:116:10
FolderWatcher-3在fun1(C:\\\
odeCode\FolderWatcher\parse.js:393:4)
FolderWatcher-3在C:\\\
odeCode \FolderWatcher\parse.js:114:8
C:\\\
odeCode\FolderWatcher\\\
ode_modules\async\dist\async.js中的FolderWatcher-3:4637:20
FolderWatcher-3在补充(C:\\\
odeCode\FolderWatcher\\\
ode_modules\async\dist\async.js:871:21)
FolderWatcher-3在C:\\\
odeCode\FolderWatcher\\ \\ node_modules\async\dist\async.js:881:15
每个Limit的FolderWatcher-3(C:\\\
odeCode\FolderWatcher\\\
ode_modules\async\dist\async.js: 4662:33)
ObjectWatcher-3在Object。< anonymous> (C:\\\
odeCode\FolderWatcher\\\
ode_modules\async\dist\async.js:930:20)
FolderWatcher-3在进程(C:\\\
odeCode\FolderWatcher\parse。 js:87:10)

调用功能

  // fun1 
// var eventJSON = {};
if(eventJSON.fun1 === undefined){eventJSON.fun1 = [];}
fun1(frameBuffer,ushort_FrameType,function(err,result){// ln 114
if(错误){
callback(err); // ln 116
} else {
eventJSON.fun1.push(result);
callback(null);
}
});


解决方案

我想我已经想到了...



cb(null,jsonData); 移动到try catch块的外面, catch(err)而不是之前,我看不到同样的错误。

  function fun1(buffer_1,ushort_Type,cb ){
cb =(typeof cb ==='function'?cb:function(){});
var jsonData = {};

try {
var uint_val1 = buffer_1.readUInt32LE(4);
var string1_val2 = buffer_1.toString('utf8',12,45);
var ubyte_val3 = buffer_1.readUInt8(46);

jsonData.Type = ushort_Type;
jsonData.val1 = uint_val1;
jsonData.val2 = string1_val2;
jsonData.val3 = ubyte_val3;
} catch(err){
return cb(err);
}
cb(null,jsonData);
}


I am still new to nodejs and developing my own asynchronous functions. According to the stack trace, I am looking at, I am being told that the following code is getting called back twice. Specifically the catch callback.

Is there a better way to structure this so if more than one of the variables in the try blows up it only calls back once?

As near as I can tell, since all of the buffer reads are done asynchronously if more than one error out they would all call the catch pretty much at the same time causing my error. At least that is the only thing I can think of that would cause this error, but for the life of me, I can't think of a way around it.

function fun1(buffer_1, ushort_Type, cb){
    cb = (typeof cb === 'function' ? cb : function(){} );
    var jsonData = {};

    try{
        var uint_val1 = buffer_1.readUInt32LE(4);
        var string1_val2 = buffer_1.toString('utf8', 12, 45);
        var ubyte_val3 = buffer_1.readUInt8(46);

        jsonData.Type = ushort_Type;
        jsonData.val1 = uint_val1;
        jsonData.val2 = string1_val2;
        jsonData.val3 = ubyte_val3;

        cb(null, jsonData);
    }catch(err){
        cb(err);  //ln 393
    }
}

Error Stack trace.

FolderWatcher-3 [26/01/2017 17:16:45.898] [ERROR] Error: Callback was already called.
FolderWatcher-3     at C:\nodeCode\FolderWatcher\node_modules\async\dist\async.js:837:36
FolderWatcher-3     at C:\nodeCode\FolderWatcher\parse.js:116:10
FolderWatcher-3     at fun1 (C:\nodeCode\FolderWatcher\parse.js:393:4)
FolderWatcher-3     at C:\nodeCode\FolderWatcher\parse.js:114:8
FolderWatcher-3     at C:\nodeCode\FolderWatcher\node_modules\async\dist\async.js:4637:20
FolderWatcher-3     at replenish (C:\nodeCode\FolderWatcher\node_modules\async\dist\async.js:871:21)
FolderWatcher-3     at C:\nodeCode\FolderWatcher\node_modules\async\dist\async.js:881:15
FolderWatcher-3     at eachLimit (C:\nodeCode\FolderWatcher\node_modules\async\dist\async.js:4662:33)
FolderWatcher-3     at Object.<anonymous> (C:\nodeCode\FolderWatcher\node_modules\async\dist\async.js:930:20)
FolderWatcher-3     at process (C:\nodeCode\FolderWatcher\parse.js:87:10)

Calling Function

    //fun1
    // var eventJSON = {};
    if (eventJSON.fun1 === undefined) { eventJSON.fun1 = [];}
    fun1(frameBuffer, ushort_FrameType, function(err, result){  //ln 114
        if(err){
            callback(err);    //ln 116
        }else{
            eventJSON.fun1.push(result);
            callback(null);
        }
    });

解决方案

I think I have it figured out....

By moving the cb(null, jsonData); to just outside the try catch block so it happens after the catch(err) instead of before, I am not seeing the same error any more.

function fun1(buffer_1, ushort_Type, cb){
    cb = (typeof cb === 'function' ? cb : function(){} );
    var jsonData = {};

    try{
        var uint_val1 = buffer_1.readUInt32LE(4);
        var string1_val2 = buffer_1.toString('utf8', 12, 45);
        var ubyte_val3 = buffer_1.readUInt8(46);

        jsonData.Type = ushort_Type;
        jsonData.val1 = uint_val1;
        jsonData.val2 = string1_val2;
        jsonData.val3 = ubyte_val3;
    }catch(err){
        return cb(err);
    }
    cb(null, jsonData);
}

这篇关于nodejs错误:回调已被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆