我怎样才能获得MongoDB结果到一个asynchronouscallback

是否有可能得到这个查询的结果到节点var对象“数据”? 控制台中显示正确的console.log(结果),但结果未被重新转换为数据variables。

var Metric = require('../metric'); var gViews = Object.create(Metric.prototype); gViews.name = 'g_ref'; gViews.initialData = 0; gViews.increment = function(results) { var mongodb = require('mongodb'); var server = new mongodb.Server("127.0.0.1", 27017, {}); new mongodb.Db('xxxx', server, {}).open(function (error, client) { client.collection('xxxx_1', function(err, collection) { collection.insert({ref_domain:g}, function(err, docs) { collection.find({ref_domain: /g/}).count(function(err, results) { console.log(results); }); }); }); }); this.data; }; module.exports = gViews; 

你只需要设置它。

 var Metric = require('../metric'); var gViews = Object.create(Metric.prototype); gViews.name = 'g_ref'; gViews.initialData = 0; gViews.increment = function(results) { var mongodb = require('mongodb'); var server = new mongodb.Server("127.0.0.1", 27017, {}); new mongodb.Db('xxxx', server, {}).open(function (error, client) { client.collection('xxxx_1', function(err, collection) { collection.insert({ref_domain:g}, function(err, docs) { collection.find({ref_domain: /g/}).count(function(err, results) { gViews.data = results; }); }); }); }); }; module.exports = gViews;