我正在尝试使用jwt设置非常基本的身份验证。我可以将信息直接上传到邮递员的mongodb集群中,但是尝试在前端进行相同的工作确实很艰难。

当我进入本地主机上的“ / register”时,输入信息并按submit,它将以
"name" is required

我感到很困惑,因为我以为我已经在输入文本框中提交了名称。这是我的架构:

const mongoose = require("mongoose");
const userSchema = new mongoose.Schema({
    name:{
        type:String,
        required:true,
        max:300,
        min:6
    },
    email:{
        type:String,
        required:true,
        max:255,
        min:6
    },
    password:{
        type:String,
        required:true,
        max:1024,
        min:6
    },
    date:{
        type:Date,
        default:Date.now
    }
});

module.exports = mongoose.model('User',userSchema);


这是我的index.js中的发布过程

app.post('/register',async(req,res)=>{

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

    //If user already in db
    const emailExist = await User.findOne({email:req.body.email});
    if(emailExist) return res.status(400).send('Email already exists');

    //Salt generation
    const salt = await bcrypt.genSalt(10);
    const hashPassword = await bcrypt.hash(req.body.password,salt);

    //Create User
    const u = new User({
        name:req.body.name,
        email:req.body.email,
        password:hashPassword
    });
    try{
        const savedUser = await u.save();
        res.send(savedUser);
        //If want to send just id:
        //res.send({user:user._id});
    }catch(err){
        res.status(400).send(err);
    }
});

//LOGIN:
app.post('/login',async(req,res)=>{

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

    //If user already in db
    const u = await User.findOne({email:req.body.email});
    if(!u) return res.status(400).send("Email is wrong");

    //Password check
    const user_password = u.password;
    const password_history = req.body.password;
    if(user_password != password_history) return res.status(400).send("Password is wrong");

    //JWT
    const token = jwt.sign({_id:u._id},process.env.TOKEN_SECRET);
    res.header('token',token).send(token);

});


最后是注册表,register.ejs

<h1>Register</h1>
<form action="/register" method="POST">
  <div>
    <label for="name">Name</label>
    <input type="text" id="name" name="name" >
  </div>
  <div>
    <label for="email">Email</label>
    <input type="email" id="email" name="email" >
  </div>
  <div>
    <label for="password">Password</label>
    <input type="password" id="password" name="password" >
  </div>
  <button type="submit">Register</button>
</form>
<a href="/login">Login</a>


我对服务器为何声明该"name" is required感到困惑。我的理解是,提交按钮将“ / register”发布到app.post()中,然后根据架构要求输入名称,电子邮件和密码。我以为name:req.body.name是作为注册用户时使用的名称而输入的名称。有人可以解释我为什么错吗?

有关更多上下文,我将整个项目放在plunkr上:https://plnkr.co/edit/kd1kI4fZGGJnmarSh0pq?p=catalogue

最佳答案

我想到了。问题是我提交的是json数据,而不是form-data。解决此问题的方法是导入body-parser,删除行app.use(express.json());,然后将其替换为app.use(bodyParser.urlencoded({ extended: false }))。我检查了我的mongo集群,然后瞧瞧该用户已注册到数据库中。

10-07 19:00
查看更多