can not use a writeConcern without a provided callback
发布于 11 年前 作者 384324085 5992 次浏览 最后一次编辑是 8 年前

今天做到注册,还是按照书上一步一步来,后来发现注册的相应post函数跟发送微博的post函数重名了,于是乎,就该改了注册的post函数名为doReg,修改时,我将routes和app.js中相对应的字段都进行了修改,当我再次启动服务器,出现上述错误。我找到最近的文件查找问题。以下是module中自定义的user模块

User.prototype.save = function save(callback){
 var user = {
 	name: this.name,
 	password: this.password
 	};
 mongodb.open(function(err,db){
 	if(err){
   return callback(err);
   }
 	db.collection('users',function(err,collection){
   if(err){
    mongodb.close();
    return callback(err);
    }
   collection.ensureIndex('name',{unique: true});
   collection.insert(user,{safe: true},function(err,user){
    mongodb.close();
    callback(err,user);
    });
   });
 	})
 };

提示是

db.collection('users',function(err,collection)这句和collection.ensureIndex('name',{unique: true});

这句有问题。

4 回复

谷歌后,找到了 将collection.ensureIndex(‘name’,{unique: true});改成: collection.ensureIndex(‘name’,{unique: true},{w: 0}); 这种该法是你不考虑跟数据库处理的结果,如果担心出错,你可以加一个回调函数。 原文地址:http://stackoverflow.com/questions/14407834/error-when-inserting-a-document-into-mongodb-via-node-js (ps:话说,这个小问题,蛋疼的好久啊)

hello,小菜问个问题:<Node.js开发指南>中微博例子的搭建过程中,mongodb的使用时用到:

db.collection('users',function(){});

这样去取’users’这个集合,但是整个项目都没有看到在哪里声明了这个集合,求指教

Existing collections can be opened with collection

db.collection([[name[, options]], callback); If strict mode is off, then a new collection is created if not already present.

貌似改成这样还是不行啊,报object is not function 的错误。我改成这样就好了 collection.ensureIndex(‘name’,{unique: true}, function(err){});

回到顶部