2016-06-08 75 views
0

我在我的node.js项目中的以下代码。异步.eachLimit回调错误不叫

async.eachLimit(dbresult, 1, function (record, callback) { 
    var json = JSON.stringify(record) 
    var form = new FormData() 
    form.append('data', json) 
    form.submit(cfg.server + '/external/api', function (err, res) { 
    if (err) { 
    callback(err) 
    } 
    if (res.statusCode === 200) { 
     connection.query('UPDATE selected_photos set synced = 1 WHERE selected_id = "' + record.selected_id + '"', function (err, result) { 
     if (err) { 
      console.log(err) 
      callback(err) 
     } else { 
     callback() 
     } 
     }) 
    } else { 
     console.log(res.statusCode) 
    return callback(err) 
    } 
    }) 
}, function (err) { 
// if any of the file processing produced an error, err would equal that error 
if (err) { 
    // One of the iterations produced an error. 
    // All processing will now stop. 
    console.log('A share failed to process. Try rerunning the Offline Sync') 
    process.exit(0) 
    } else { 
     console.log('All files have been processed successfully') 
     process.exit(0) 
    } 
}) 
} 

res.statusCode = 302所以这应该是错误的。但错误回调从未触发。如何做到这一点得到它引发的错误,使它停止eachLimit和表演的

console.log('A share failed to process. Try rerunning the Offline Sync') 

回答

1

您有:

if (err) {

形式的第一行提交处理。之后,你确定没有错误。因此,当您检查响应statusCode并尝试回拨出错时,您正在用空值回电。 这就是为什么你在最终回调函数中检查它时没有得到错误的原因。

尝试从表单提交处理程序回调时创建new Error('Status not OK: ' + res.statusCode)

+0

让我接近。我做了:console.log(res.statusCode) err =错误('状态不正常:'+ res.statusCode) callback(err) –