背景:我想做一个个人博客网站 目的:学习nodejs+mongodb等web开发技术 个人水平:2012年毕业,12-14年都是做的Linux 上面的Netfilter的开发 14年来到上海,做了一名 salesforce的developer,当然,做这种基于平台的二次开发,我发现自己有太多的web知识要学习 像jquery,html, 还有对web开发的整体概念…等等,感觉自己是个小学生了.
环境: 系统是Mac
node : v0.12.0
nongodb:v3.0.0
├── [email protected]
├─┬ [email protected]
│ ├─┬ [email protected]
│ │ └── [email protected]
│ ├── [email protected]
│ └── [email protected]
├── [email protected]
├─┬ [email protected]
│ ├── [email protected]
│ ├─┬ [email protected]
│ │ └── [email protected]
│ ├─┬ [email protected]
│ │ ├── [email protected]
│ │ ├── [email protected]
│ │ ├─┬ [email protected]
│ │ │ ├─┬ [email protected]
│ │ │ │ ├── [email protected]
│ │ │ │ ├── [email protected]
│ │ │ │ ├── [email protected]
│ │ │ │ └── [email protected]
│ │ │ └── [email protected]
│ │ ├── [email protected]
│ │ ├── [email protected]
│ │ ├── [email protected]
│ │ ├── [email protected]
│ │ └── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ └─┬ [email protected]
│ └── [email protected]
├── [email protected]
└─┬ [email protected]
├─┬ [email protected]
│ ├─┬ [email protected]
│ │ └─┬ [email protected]
│ │ └── [email protected]
│ ├─┬ [email protected]
│ │ └── [email protected]
│ ├─┬ [email protected]
│ │ └── [email protected]
│ └── [email protected]
└─┬ [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
└── [email protected]
代码是我在github上clone的,源码地址 https://github.com/shanelau/NodeJs-Blog ** 问题:** 1,页面上有一个注册的表单提交,输入用户名和密码,点击注册,执行save
var mongodb = require('./../db')
, format = require('util').format;
function User(user) {
this.name = user.name;
this.password = user.password;
};
module.exports = User;
User.prototype.save = function save(callback) {
// 存入 Mongodb 的文档
var user = {
name: this.name,
password: this.password
};
mongodb.open(function(err, db) {
if (err) {
console.log(format("err = %s", err));
return callback(err);
}
// 读取 users 集合
db.collection('users', function(err, collection) {
if (err) {
console.log(format("err = %s", err));
mongodb.close();
return callback(err);
}
// 为 name 属性添加索引
collection.ensureIndex('name', {unique: true},{w:1});
// 写入 user 文档
collection.insert(user, {safe: true}, function(err, user) {
mongodb.close();
console.log(format("err = %s", err));
callback(err, user);
});
});
});
};
结果: 用户是注册成功了,我去数据库查看确实有这么一个刚注册的用户,但是 我的APP挂掉了 报错信息如下:
{ username: '9', password: '9', password_repeat: '9' }
POST /reg 302 229ms - 58b
err = MongoError: Cannot use a writeConcern without a provided callback
/Users/zhubaojun/GitHub/devBlog/node_modules/mongodb/lib/utils.js:99
throw err;
^
Error
at Object.<anonymous> (/Users/zhubaojun/GitHub/devBlog/node_modules/mongodb/node_modules/mongodb-core/lib/error.js:42:24)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at Object.<anonymous> (/Users/zhubaojun/GitHub/devBlog/node_modules/mongodb/node_modules/mongodb-core/index.js:2:17)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
其中err = MongoError: Cannot use a writeConcern without a provided callback 是我在 node_modules/mongodb/lib/utils.js 种这个函数里面加了一个报错信息
console.log(format("err = %s", err));
,才报这条错误信息的
var format = require('util').format;
var handleCallback = function(callback, err, value1, value2) {
try {
if(callback == null) return;
if(value2) return callback(err, value1, value2);
return callback(err, value1);
} catch(err) {
process.nextTick(function() {
console.log(format("err = %s", err));
throw err;
});
return false;
}
return true;
}
我去Google上搜索,是需要一个callback函数,我想不出在哪里添加,因为看代码我觉得callback函数已经添加了 请问该怎么追踪错误和 修改代码,谢谢