不能在Nodejs中使用Mongodb

时间:2017-02-14 17:29:05

标签: node.js mongodb

当我尝试从集合中的字段(" acc")中获取所有不同的值时,我遇到了问题

var mongojs = require('mongojs');
var mongodb = mongojs('mongodb://localhost:27017/na');
var mongodb_data = mongodb.collection('data');


mongodb_data.distinct(
   "acc",
   (function(err, docs){
        if(err){
            return console.log(err);
        }
        if(docs){  
            console.log(docs);
        }
   })
);

当我运行代码时,它给出了下面的错误跟踪

(node:45480) DeprecationWarning: sys is deprecated. Use util instead.
/.../node_modules/mongodb/lib/utils.js:98
process.nextTick(function() { throw err; });
                              ^
TypeError: cb is not a function
    at /U.../node_modules/mongojs/lib/collection.js:69:5
    at handleCallback (/.../node_modules/mongodb/lib/utils.js:95:56)
    at /.../node_modules/mongodb/lib/db.js:313:5
    at /.../node_modules/mongodb-core/lib/connection/pool.js:455:18
    at _combinedTickCallback (internal/process/next_tick.js:67:7)
    at process._tickCallback (internal/process/next_tick.js:98:9)

1 个答案:

答案 0 :(得分:2)

query方法接受mongodb_data.distinct( "acc", {}, // query object (function(err, docs){ if(err){ return console.log(err); } if(docs){ console.log(docs); } }) ); 对象作为第二个参数,试试这个:

{{1}}