Node.js MongoDB - collection.find()。toArray(回调)无法执行

时间:2015-06-21 01:59:52

标签: mysql node.js mongodb

我想从MongoDB读取数据并将它们写入MySQL。 Mongo中有很多行,我认为应该使用它们将它们插入到MySQL中,但我不知道我是对的。

我的代码是这样的:

MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
    console.log(Date() + "mongoDb connect success");

    async.parallel([
        function() {
            conn.query(selectAllOrders, function(error, rows, fields) {
                if (error)
                    console.log(Date() + 'error');

                console.log(Date() + ' row length:', rows.length);
                async.eachSeries(rows, function(order) {
                    findTakerLocus(db, 'position_2015_06_15', {}, function(res) {
                        console.log(res);
                    });

                }, function(error) {
                    console.log(Date() + 'error:' + error);
                });

                console.log(Date() + 'you are not right');
            });
        },
        function() {
            db.close();
            console.log(Date() + 'mongoDb关闭连接');
            conn.end();
            console.log(Date() + 'mysql 连接关闭');
        }
    ], function(err, results) {
        log('1.1 err: ', err);
        log('1.1 results: ', results);
    });

});

var findTakerLocus = function(db, collectionName, key, callback) {
    var collection = db.collection(collectionName);

    collection.find(key).toArray(function(err, docs) {
        console.log(' excute callback');
        callback(docs);
    });
}

0 个答案:

没有答案