2016-06-08 73 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) –