search

Home  >  Q&A  >  body text

node.js - nodejs用户注册的信息在数据库里面显示不出来

学node没多久,准备用node做个注册的功能, 但是注册页面的信息提交不到数据库,数据库用的mongodb,

注册页面

<p class="col-sm-8 col-sm-offset-1 main pull-left">
    <form class="form-horizontal" action="/signup" method="post" enctype="multipart/form-data">
        <p class="form-group">
            <label for="inputName" class="col-sm-2 control-label">用户名</label>
            <p class="col-sm-4">
                <input type="text" class="form-control" id="inputName" name="username">
            </p>
        </p>
        <p class="form-group">
            <label for="inputPassword3" class="col-sm-2 control-label">密码</label>
            <p class="col-sm-4">
                <input type="password" class="form-control" id="inputPassword3" name="password">
            </p>
        </p>
        <p class="form-group">
            <label for="" class="col-sm-2 control-label">再次输入密码</label>
            <p class="col-sm-4">
                <input type="password" class="form-control" id="" name="re_password">
            </p>
        </p>
        <p class="form-group">
            <label for="inputEmail" class="col-sm-2 control-label">邮箱</label>
            <p class="col-sm-4">
                <input type="email" class="form-control" id="inputEmail" name="email">
            </p>
        </p>
        <p class="form-group">
            <p class="col-sm-offset-2 col-sm-4">
                <button type="submit" class="btn btn-default">注册</butto>
            </p>
        </p>
    </form>
</p>

注册页面路由

// 注册页面
router.get('/signup', function(req, res, next){
  res.render('sign/signup');
});
router.post('/signup', function(req, res, next){
  // 获取数据
  var name = req.body.username;
  var pwd = req.body.password;
  var re_pwd = req.body.re_password;
  var email = req.body.email; 

  // 存入数据库
  User.create({
    userName: name,
    usePwd: pwd,
    userEmail: email
  },(err) => {
    if (err) next (err);
    res.redirect('/signin');
  })
});

数据模型

const mongoose = require(‘mongoose’);

mongoose.connect(‘mongodb://localhost/cnode’);

var schema = new mongoose.Schema({
name: String,
pwd: Number,
email: String
});

module.exports = mongoose.model(‘User’, schema);

填完注册的信息后,成功的跳到了登录页面,但是这个时候数据库确实这样的

尝试了四次全是这样,这是怎么回事

高洛峰高洛峰2896 days ago1008

reply all(3)I'll reply

  • PHPz

    PHPz2017-04-17 16:40:05

    Remove enctype="multipart/form-data" in the registration form

    reply
    0
  • 阿神

    阿神2017-04-17 16:40:05

    I seem to see the problem.
    First of all,

      // 存入数据库
      User.create({
        userName: name,
        usePwd: pwd,
        userEmail: email
      },(err) => {
        if (err) next (err);
        res.redirect('/signin');
      })

    The attribute names used are userName, usePwd, useEmail,
    but in the schema definition it becomes name, pwd, email

    reply
    0
  • 迷茫

    迷茫2017-04-17 16:40:05

    The poster’s code is not completely posted. From the existing code, I guess that the field name when defining the schema is inconsistent with the field name of the object passed in when creating the schema instance?

      // 存入数据库
      User.create({
        userName: name,
        usePwd: pwd,
        userEmail: email
      },(err) => {
        if (err) next (err);
        res.redirect('/signin');
      })
      User.create({
        userName: name,
        usePwd: pwd,
        userEmail: email
      },(err) => {
        if (err) next (err);
        res.redirect('/signin');
      })

    reply
    0
  • Cancelreply