(function() {
var User, mongodb;
mongodb = require('./db');
User = function(user) {
this.name = user.name;
this.pass = user.pass;
console.log('toString name -> ' + this.name + 'pass -> ' + this.pass);
};
User.prototype.mongodbSave = function(callback) {
var user;
user = {
name: this.name,
pass: this.pass
};
mongodb.open(function(err, db) {
if (err) {
return callback(err);
}
db.collection('users', function(err, collection) {
if (err) {
mongodb.close();
return callback(err);
}
collection.insert(user, {safe: true}, function(err, user) {
mongodb.close();
console.log(user)
// callback(err,user);
});
});
});
};
// User.mongodbfind = function(username, callback) {
// mongodb.open(function(err, db) {
// if (err) {
// return callback(err);
// }
// db.collection('users', function(err, collection) {
// if (err) {
// mongodb.close();
// return err;
// }
// collection.findOne({
// name: username
// }, function(err, doc) {
// var user;
// mongodb.close();
// if (doc) {
// user = new User(doc);
// console.log(user);
// callback(err, user);
// } else {
// callback(err, null);
// }
// });
// });
// });
// };
module.exports = User;
}).call(this);
加入了callback 报错显示callback方法不存在...可是最外层的参数以已经有了阿。。求高手解答