Express 如何在Node.js中不保存相同的电子邮件用户?
我想注册用户,但没有相同的电子邮件 如果电子邮件已经存在,他们将无法注册 由于我的电子邮件字段是一个数组,我不知道如何处理这个问题Express 如何在Node.js中不保存相同的电子邮件用户?,express,mongoose,Express,Mongoose,我想注册用户,但没有相同的电子邮件 如果电子邮件已经存在,他们将无法注册 由于我的电子邮件字段是一个数组,我不知道如何处理这个问题 我收到一个错误“无法读取未定义的“email”属性”使用嵌套搜索条件: let user=wait user.findOne({'emailInfo.email':emailInfo.email}); const UserSchema = new mongoose.Schema({ firstname: { type: String, },
我收到一个错误“无法读取未定义的“email”属性”使用嵌套搜索条件:
let user=wait user.findOne({'emailInfo.email':emailInfo.email});
const UserSchema = new mongoose.Schema({
firstname: {
type: String,
},
lastname: {
type: String,
},
emailInfo: [
{
email: {
type: String,
unique: true,
},
emailType: String,
},
],
password: {
type: String,
},
registerNewUser: async (req, res) => {
console.log(req.body);
const { firstname, lastname, emailInfo, password} = req.body;
try {
let user = await User.findOne({ email: emailInfo.email });
if (user) {
return res.status(400).json({ msg: "This email id is already in use" });
}
user = new User({
firstname,
lastname,
emailInfo,
password,
});
const { emailType, email } = req.body;
const emailList = { emailType, email };
user.emailInfo.unshift(emailList);
const salt = await bcrypt.genSalt(10);
user.password = await bcrypt.hash(password, salt);
await user.save()
}