I have the following model. When I try to create with wrong information it doesn't allow but if I try to edit the information allows it. How can I prevent that?
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,
});
updateValidators are off by default, you need to specify runValidators: true
option in the update operation.
userController.updateUserPassword = async (req, res) => {
try {
const oldUser = await User.findByIdAndUpdate(
req.params.userId,
{
password: req.body.password,
},
{
runValidators: true,
}
);
//nao permitir password vazia
const newUser = await User.findById(req.params.userId);
res.send({
old: oldUser,
new: newUser,
});
} catch (err) {
console.log('Error: ', err);
res.status(500).send('Something went wrong.');
}
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.