nodejs Error: Callback was already called

2019-09-14 06:11发布

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);
        }
    });

1条回答
劫难
2楼-- · 2019-09-14 06:43

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);
}
查看更多
登录 后发表回答