2014-09-04 32 views
0

我有一個for循環插入文件在MongoDB中。使用異步系列我運行五次循環,然後關閉連接。由於一些奇怪的原因,當第六次執行循環時,我得到一個插入錯誤。我只是不確定這裏發生了什麼。有任何想法嗎?插入錯誤使用node.js異步系列

var mycollection= "abcd"; 
var count = 5; 
var db; 
var col; 

async.series([ 
// Connect to DB 
function(callback) { 
    MongoClient.connect("mongodb://connection-path-here",function(error, db2) { 
     if (error) {console.log("db connect error");callback(error,"db connect error"); return;} 
     db = db2; 
     callback(null,"connect success"); 
    }); 
}, 
function(callback) { 
    col = db.collection(mycollection); 
    callback(null,"collection success"); 
}, 
function(callback) { 
    console.log ("insert begin ...");    
    for (var i = 1; i <= count; i++) { 
    console.log("inserting ....." + i); 
    col.insert({c:i}, function(error,result) { 
     if (error) {console.log("insert error:" + i);callback(error,"insert error"); return;} 
    }); 
    } 
callback(null,"insert success"); 
}, 
function (callback){ 
    console.log ("close db");db.close(); 
    callback(null,"connection closed"); 
} 
], function(error, results) { 
    if (error) { console.log("error"); } 
     console.log(results); 
}); 

輸出

insert begin ... 
inserting .....1 
inserting .....2 
inserting .....3 
inserting .....4 
inserting .....5 
close db 
insert error:6 
error 
    [ 'connect success', 'collection success', 'insert error' ] 
insert error:6 
insert error:6 
insert error:6 
insert error:6 

回答

1

你就開始了,但你在這裏混合異步和非異步方法:

function(callback) { 
    console.log ("insert begin ...");    
    for (var i = 1; i <= count; i++) { 
    console.log("inserting ....." + i); 
    col.insert({c:i}, function(error,result) { 
     if (error) {console.log("insert error:" + i);callback(error,"insert error"); return;} 
    }); 
    } 
callback(null,"insert success"); 
}, 

這不等待插入在迭代下一個循環之前完成,所以你需要等待的東西。嘗試「async.whilst」:

function(callback) { 
    var i = 1; 
    async.whilst(
     function() { return i <= count }, 
     function(callback) { 
      col.insert({ c: i },function(error,result) { 
       if (error) 
        console.log("insert error:" + i); 
       i++; 
       callback(error); 
      }); 
     }, 
     function(error) { 
      callback(error,"insert sucess") 
     } 
    ); 
}, 

其餘部分可以按照您的操作進行,結果將發送到系列執行結束。

0

您所有的異步操作的應該是您正在執行的「操作」數組中的一個功能。您的for循環調用col.insert()6次,但在第6次操作之前可以完成調用callback(null,「insert success」) - 這將導致競爭條件,所以有時它可能會實際上完成或可能它只會插入一些記錄,取決於每個插入的時間。

請參見下面的修改後的代碼來解決該問題:

var mycollection= "abcd"; 
var count = 5; 
var db; 
var col; 

// create operations array with first two operations 
var operations = [ 
    // Connect to DB 
    function(callback) { 
    MongoClient.connect("mongodb://connection-path-here",function(error, db2) { 
     if (error) {console.log("db connect error");callback(error,"db connect error"); return;} 
     db = db2; 
     callback(null,"connect success"); 
    }); 
    }, 
    function(callback) { 
    col = db.collection(mycollection); 
    callback(null,"collection success"); 
    } 
]; 


// push each insert into operations as its own individual operation 
for (var i = 1; i <= count; i++) { 
    operations.push(function(callback){ 
    console.log("inserting ....." + i); 
    col.insert({c:i}, function(error,result) { 
     if (error) {console.log("insert error:" + i);callback(error,"insert error"); return;} 
     // no errors here, so this insert was successful! 
     callback(null); 
    }); 
    }) 
} 

// finish pushing the last two operations 
operations.push(function(callback) { 
    callback(null,"insert success"); 
}); 

operations.push(function (callback){ 
    console.log ("close db");db.close(); 
    callback(null,"connection closed"); 
}); 

// execute all the operations 
async.series(operations, function(error, results) { 
    if (error) { console.log("error"); } 
    console.log(results); 
});