gpt4 book ai didi

javascript - 密码不允许nodejs + mongodb

转载 作者:行者123 更新时间:2023-12-01 00:13:35 25 4
gpt4 key购买 nike

我正在使用 Node js 和 mongodb 创建一个 Web api。我尝试创建一条路线,当我与 postman 检查时,它说

不允许使用“密码”

这是我使用过的代码

路线

router.post('/adminregister', upload.single('profileImage'), async(req, res) => {

const { error } = registerValidation(req.body);
if (error) return res.status(400).send(error.details[0].message);

const emailExists = await User.findOne({ email: req.body.email });
if (emailExists) return res.status(400).send('Email Already Exists');

const user = new User({
name: req.body.name,
gender: req.body.gender,
bday: req.body.bday,
email: req.body.email,
phone: req.body.phone,
image: req.file.path,
password: req.body.password
});
try {
const savedUser = await user.save();

const token = jwt.sign({ _id: user._id }, process.env.TOKEN_SECRET);

res.header('auth-token', token).send({
loginstatus: 'olduser',
token: token
});
} catch (err) {
res.status(400).send(err);
}});

这是用户架构

const userSchema = new mongoose.Schema({
name: {
type: String,
required: true,
min: 5
},
gender: {
type: String,
required: true
},
bday: {
type: Date,
required: true
},
email: {
type: String,
required: true,
max: 255,
min: 6
},
phone: {
type: String,
required: true,
min: 6
},
image: {
type: String
// required:true
},
password: {
type: String
},
usertype: {
type: String,
default: 'user'
},
status: {
type: String,
required: true,
default: 'active'
}});

当我通过 postman 提出请求时,它会给我这个

但是当我从 postman 那里删除密码时,它工作正常

enter image description here

最佳答案

unknown(true) 添加到您的 Joi 架构中,以允许请求正文中包含其他关键字

validationSchema:Joi.object().keys({ 
name: Joi.string().required(),
...
}).unknown(true)

关于javascript - 密码不允许nodejs + mongodb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59920749/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com