【发布时间】:2020-08-26 05:35:49
【问题描述】:
我有以下型号。当我尝试使用错误信息创建时,它不允许,但如果我尝试编辑信息,则允许它。我怎样才能防止这种情况发生?
var userSchema = new Schema({
cartaoCidadao: {
type: String,
required: true,
index: {
unique: true,
},
match: /[0-9]{8}/,
},
password: { type: String, required: true },
histórico: [
{
type: Schema.Types.ObjectId,
ref: "Request",
},
],
role: { type: String },
estado: { type: String, enum: ["Infetado", "Suspeito", "Curado"] },
});
userController.updateUserPassword = async (req, res) => {
const oldUser = await User.findByIdAndUpdate(req.params.userId, {
password: req.body.password,
});
//nao permitir password vazia
const newUser = await User.findById(req.params.userId);
res.send({
old: oldUser,
new: newUser,
});
};
userController.updateUserState = async (req, res) => {
const oldUser = await User.findByIdAndUpdate(req.params.userId, {
estado: req.body.estado,
});
【问题讨论】:
标签: javascript node.js express mongoose model