按照某前辈的的mongo用法写了以下代码 collection.update({name:“tom”}, {$set:{name:“Jim”}}); 可是总是提示我 Cannot call method ‘update’ of null 为啥呢!
贴一下完整的db操作的代码吧~是不是没有在回调里面call
var mongodb = require(“mongodb”);
var mongoserver ; //Mongo对象
var db_connector ;//DB对象
var db_name=‘hummer_mongo’;
var db_ip=‘127.0.0.1’;
/插入一条新数据/ function insert(a_where,a_data) { db_connector.createCollection(a_where, function(err, collection){ collection.insert(a_data); console.log(“insert over1.\n”+a_where);
}); return true; }
/查询/ function select(a_where,a_data) { db_connector.collection(a_where, function(err, collection) { collection.find(a_data, function(err, value){ value.toArray(function(err,arr){ console.log(arr); return arr; }) }); }); return true; }
/更新/ function updataex(a_where,a_data) { db_connector.collection(a_where, function(err, collection) {
collection.update({name:"tom"}, {$set:{name:"Jim"}});
}) console.log(‘updata’); return true; }
/*创建连接/ function connector_monggo(/a_ip,a_list_name/) {
mongoserver = new mongodb.Server(db_ip, 27017,{auto_reconnect:true}); //连接Mongodb
db_connector = new mongodb.Db(db_name, mongoserver);
db_connector.open(function(err,db_connector){
if(!err)
{
console.log('connect');
db_connector.close();
mongoserver.close();
}else{
console.log(err);
}
})
console.log('connector_monggo');
} exports.select=select; exports.insert=insert; exports.updataex=updataex; exports.connector_monggo=connector_monggo;
@beiyio 调试一下看看err是什么