我有一个简单的小用户注册表,如下所示:
// POST Register new user
exports.new = function(req, res) {
var db = require('mongojs').connect('localhost/busapp', ['users']);
db.users.ensureIndex({email:1}, {unique: true})
function User(email, username, password, dateCreated) {
this.email = email;
this.username = username;
this.password = password;
this.dateCreated = new Date();
this.admin = 0;
this.activated = 0
}
if (req.body.user.password !== req.body.user.passwordc) {
res.send('Passwords do not match');
} else {
var user = new User(req.body.user.email, req.body.user.username,
req.body.user.password);
// TODO: Remove this after we clarify that it works.
console.log(user.email + " " + user.username + " " +
user.password);
// Save user to database
db.users.save(user, function(err, savedUser) {
if (err) {
res.send(err);
} else {
console.log("User " + savedUser.email + " saved");
}
});
}
}
但是我在验证提交的信息时遇到了麻烦,比如唯一值是空的,诸如此类,所以没有人可以向数据库发送 post 请求以绕过 jQuery 验证功能。我已通读文档,但似乎无法正确理解。我尝试设置一个 ensureIndex,但是,这似乎不起作用。任何有关如何验证数据库端输入的信息都将非常感谢!