2

Mongo DB のコレクションからレコードの値を取得しようとしています

これが私の情報源です。

exports.procc = function(req, res) {

        db.collection('search', function(err, collection) {
                    collection.count({'str':'car'},function(err, count) {
                       console.log(count+' records');//Prints 2 records
                       c=count;     
                    });
        });
        console.log('records= '+c);//print 0 records
 res.end();
};

問題は、コールバックからレジスタの番号が出力されることですが、コールバックからは 0 が出力され、その値を変数に保存する方法がわかりません。

4

2 に答える 2

0

あなたのコードは、node.js IO の非同期の性質を考慮していません。

exports.procc = function(req, res) {
        db.collection('search', function(err, collection) {
                    collection.count({'str':'car'},function(err, count) {
                       console.log('records= ' + count);//print 0 records
                       res.end();
                    });
        });
};
于 2013-08-26T14:05:28.480 に答える