在百度开放平台搭一个nodejs网站,mongodb连不上,测试时 报错 cannot call method ‘authenticate’ of null,各位大神怎么解啊 var Db = require(‘mongodb’).Db; var Server = require(‘mongodb’).Server; /数据库连接信息host,port,user,pwd/ var db_name = ‘BKaUeLhcUNxDcmbjTcAR’; // 数据库名,从云平台获取 var db_host = ‘mongo.duapp.com’;// 数据库地址 var db_port = ‘8908’; // 数据库端口 var username = ‘4vZ6xZRb8uHpmxPL6XSWFKHo’;// 用户名(API KEY) var password = ‘XXXXXXXX’;// 这个就保密啊 function testMongo(req, res) { var db = new Db(db_name, new Server(db_host, db_port, {}), {w: 1}); db.open(function(err, db) { db.authenticate(username, password, function(err, result) { if (err) { db.close(); console.log(‘Authenticate failed!’); } console.log(‘Authenticate succeed!’); }); }); } testMongo()
cannot call method ‘authenticate’ of null 意思是 db.authenticate这个db这个变量为null,所以不行
如何解决呢