即使我对该字段密码的稀疏索引设置为true,我仍然遇到此错误 这是用户架构:
var roledef = 'Member';
var mongoose = require('mongoose');
var UserSchema = new mongoose.Schema({
username: {
type: String,
required: true,
unique: true,
sparse: true,
minlength: [5, 'Username must be grater than 5 characters']
},
password: {
type: String,
required: true,
sparse: true,
minlength: [5, 'Username must be grater than 5 characters']
},
email: {
type: String,
required: true
},
profileid: {
type: String,
required: true,
},
roles: {
type: String,
default: roledef,
sparse: true
}
});
const User = mongoose.model('User', UserSchema);
module.exports = User;
// write encryption
我正在路由一个有效的索引文件 但我不知道这是否是创建用户的最佳方法 这是我的用户控制器文件:
const userModel = require('../../models');
const UserController = {};
const User = require('../../models/User')
const sha256 = require('sha256');
module.exports.validateregistration = (req, res) => {
console.log("##########################################################".red);
var ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress
if (req.body.encodedUsername && req.body.encodedPassword && req.body.encodedEmail) {
// res.send(result);
log(ip);
var buffU = new Buffer(req.body.encodedUsername, 'base64');
var buffP = new Buffer(req.body.encodedPassword, 'base64');
var buffE = new Buffer(req.body.encodedEmail, 'base64');
var Username = buffU.toString('ascii');
var Pass = buffP.toString('ascii');
var Email = buffE.toString('ascii');
var hashPass = sha256.x2(Pass);
console.log("Register request for " + "Username: " + Username);
const userData = {
username: Username,
password: hashPass,
email: Email,
profileid: sha256(buffU)
};
const user = new userModel.User({
username: Username,
password: hashPass,
email: Email,
profileid: sha256(buffU)
});
const details = userData;
user.save().then((newuser) => {
res.status(200).json({
success: true,
data: newuser
});
}).catch((err) => {
res.status(500).json({
message: err
});
console.log(err);
});
// User.findOne({ username: userData.username, email: userData.email }, (err, resultofcheck) => {
// console.log(resultofcheck)
// if (resultofcheck == null) {
// User.create(userData, function (err, user) {
// if (err) {
// console.log(err);
// res.send("2")
// } else {
// console.log(user);
// console.log("New user created: " + user.username + " Profile id: " + user.profileid);
// res.redirect("/api/profile/" + user.profileid);
// }
// });
// } else {
// console.log(Username + " is taken")
// console.log(err);
// res.send("2");
// };
// });
}
console.log("##########################################################".red);
};
// if (!result) {
// db.collection('users').insert(details, (err, resultofinsert) => {
// if (err) {
// console.log(err);
// res.sendStatus(500);
// } else {
// res.send(user.profileid);
// console.log("New user created: " + user);
// }
// });
// };
// db.collection('users').insert(user, (err, result) => {
// if (err) {
// res.sendStatus(501);
// res.send({ 'error': 'An error has occured!' });
// } else {
// // res.send(result.ops[0].pass);
// console.log(result);
// };
// });
我正在通过另一个索引文件进行调用,但是iworks也可以,但是出现此错误:
{MongoError:E11000重复键错误索引: mongo.users。$ password_1 dup键:{: “ e701ea082879498082d025e0cf9857ec0d19e6e86fa39f92ed3286de55d340e6”} 在Function.create(C:\ Users \ abinash \ Desktop \ api \ node_modules \ mongodb-core \ lib \ error.js:43:12) 在toError(C:\ Users \ abinash \ Desktop \ api \ node_modules \ mongoose \ node_modules \ mongodb \ lib \ utils.js:149:22) 在coll.s.topology.insert(C:\ Users \ abinash \ Desktop \ api \ node_modules \ mongoose \ node_modules \ mongodb \ lib \ operations \ collection_ops.js:828:39) 在C:\ Users \ abinash \ Desktop \ api \ node_modules \ mongodb-core \ lib \ connection \ pool.js:532:18 在_combinedTickCallback(内部/进程/next_tick.js:131:7) 在process._tickCallback(internal / process / next_tick.js:180:9)驱动程序处:true,名称:'MongoError',索引:0,代码:11000,
errmsg:'E11000重复键错误索引:mongo.users。$ password_1 dup 密钥:{: “ e701ea082879498082d025e0cf9857ec0d19e6e86fa39f92ed3286de55d340e6”}', [Symbol(mongoErrorContextSymbol)]:{}}
尽管用户名和电子邮件地址不同,并且不需要密码唯一,但我尝试使用稀疏:true,但不能正常工作,请hep指出我做错了什么 被注释的部分是我发现猫鼬模型之前反复尝试的事情