0
저는 아직 nodejs에 익숙하지 않고 제 자신의 비동기 함수를 개발하고 있습니다. 스택 추적에 따르면, 나는 다음 코드가 두 번 다시 호출되고 있다고 들었다. 특히 캐치 콜백.nodejs 오류 : 콜백이 이미 호출되었습니다.
구조를 구성하는 더 좋은 방법이 있습니까? 예를 들어 변수 중 둘 이상이 불어 나면 변수가 한 번만 호출됩니다.
내가 알 수있는 것만 큼 버퍼 읽기는 모두 비동기 적으로 수행되기 때문에 둘 이상의 오류가 발생하면 모두 캐치를 호출하여 오류가 발생합니다. 적어도 그것이 내가이 오류를 일으킬 수 있다고 생각할 수있는 유일한 것입니다. 그러나 저의 삶에 대해, 나는 그 주위에 방법을 생각할 수 없습니다.
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] [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)
ERR (단지는 캐치 후 발생하도록 시도의 catch 블록 외부 cb(null, jsonData);
를 이동하여 .... 기능
//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);
}
});
읽기가 비동기 적입니까? 설명서를 확인했지만 콜백 인수가없는 것 같습니다. fun1을 두 번 이상 호출 할 수있는 코드의 나머지 부분이 있습니까? try 문의 읽기가 비동기 인 경우 약속을 시도하고 구현하는 것이 좋습니다. 그게 내가 생각할 수있는 전부 야. –
비동기인지 아닌지는 잘 모르겠다. ... 무슨 일이 일어나고 있는지 파악할 수 없다. – shaun
eventJSON에 대해 자세히 설명해 주시겠습니까? 페이지에서 아약스를 호출 한 건가요? 말할 것도 없지만, 가장 간단한 설명처럼 fun1이 여러 번 호출되는지 파악하는 것이 나의 첫 번째 본능입니다. –