【问题标题】:Why is server saying that "name" is required when posting auth info?为什么服务器在发布身份验证信息时说需要“名称”?
【发布时间】:2020-04-17 02:40:44
【问题描述】:

我正在尝试使用 jwt 设置非常基本的身份验证。我可以在 postman 中将信息直接上传到 mongodb 集群,但我在尝试在前端发生同样的事情时遇到了非常困难的时间。

当我进入本地主机上的“/register”时,输入信息并按提交,它会回复 "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

【问题讨论】:

  • 请 console.log(req)
  • this 有帮助吗?

标签: javascript mongodb authentication mongoose schema


【解决方案1】:

我想通了。问题是我提交的是 json 数据,而不是表单数据。我为解决这个问题所做的是导入body-parser,删除app.use(express.json()); 行,并用app.use(bodyParser.urlencoded({ extended: false })) 替换它。我检查了我的 mongo 集群,发现用户已注册到数据库中。

【讨论】:

    猜你喜欢
    • 2020-04-25
    • 1970-01-01
    • 2015-05-28
    • 1970-01-01
    • 2011-09-29
    • 2019-07-24
    • 2013-12-24
    • 2018-01-22
    • 2010-10-04
    相关资源
    最近更新 更多