我已在我的应用程序中为餐厅建立了模型。但是,我很难检查数据库中是否已存在该电子邮件。这主要是因为每个管理对象都嵌套在一个数组中。 问题是我在阵列中有管理员,这使我很难 1.检查电子邮件是否存在于数据库中。 2.登录!
但是对于每个餐厅有多个管理员/用户的应用程序来说,这一点很重要。
如果您知道在MongoDB plz帮助中处理数组中的对象,那就太好了
这是注册表格的发帖路线。
router.post('/registor', (req, res, next) => {
const { adminFName, adminLName, adminEmail, adminPassword, adminPassword2, adminNumber } = req.body;
let errors = []
if (!adminFName || !adminLName || !adminEmail || !adminPassword || !adminNumber) {
errors.push({ msg: "Var vänlig fyll i alla fälten! " })
}
if (adminPassword !== adminPassword2) {
errors.push({ msg: "Lösenorden överensstämmer inte! " })
}
if (adminPassword.length < 8) {
errors.push({ msg: "Lösenordet är för kort, minst 8 tecken! " })
}
if (errors.length > 0) {
res.render('admin-registor', {
errors, adminFName, adminLName, adminEmail, adminPassword, adminPassword2, adminNumber
});
} else {
// The problem is here
Restaurant.find({ admin: { $elemMatch: { email: adminEmail } } })
.then(Restaurant => {
if (Restaurant) {
errors.push({ msg: "Email är redan registrerad!" })
res.render('admin-registor', {
errors, adminFName, adminLName, adminEmail, adminPassword, adminPassword2, adminNumber
});
} else {
const newRest = new Restaurant({
admin: [{
firstName: adminFName,
lastName: adminLName,
email: adminEmail,
password: adminPassword
}]
});
console.log(newRest);
}
});
};
这是我的餐厅模型。
adminSchema = new mongoose.Schema({
firstName: String,
lastName: String,
email: String,
username: String,
password: String,
number: String,
accessLevel: String,
isVerifide: Boolean
})
adminSchema.plugin(passportLM);
var restaurantsSchema = mongoose.Schema({
information: {
name: String,
address: String,
zip: String,
orgNumber: String,
email: String,
phoneNumber: String,
companyName: String,
Website: String,
slogan: String,
},
openHouers: {
openMon: String,
closeMon: String,
openTus: String,
closeTus: String,
openWen: String,
closeWen: String,
openThu: String,
closeThu: String,
openFri: String,
closeFri: String,
openSat: String,
closeSat: String,
openSun: String,
closeSun: String
},
categories: [{
name: String,
products: [{
name: String,
description: String,
price: Number,
glutenFree: Boolean,
lactoseFree: Boolean,
nuts: Boolean,
vegan: Boolean,
vegetarian: Boolean,
options: [{ variable: String }],
active: Boolean,
specialOffer: Boolean,
meta : {
totalSold: Number,
}
}]
}],
ingredients: [{
name: String,
description: String,
price: String,
}],
admin: [adminSchema],
meta: {
lowestPrice: Number,
weeklySales: Number,
views: Number,
stars: Number,
},
delivery: {
on: Boolean,
maxDistance: String,
price: Number,
pickUp: Boolean
}
});