javascript - 类型错误 : Cannot read property 'user_first_name' of undefined

标签 javascript mongodb express mongoose database

我正在尝试使用 expressjs/mongoose 注册用户,但出现以下错误:

TypeError: Cannot read property 'user_first_name' of undefined
at C:\Quiz webPolitica\server.js:20:24
at Layer.handle [as handle_request] (C:\Quiz webPolitica\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Quiz webPolitica\node_modules\express\lib\router\route.js:131:13)
at Route.dispatch (C:\Quiz webPolitica\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\Quiz webPolitica\node_modules\express\lib\router\layer.js:95:5)
at C:\Quiz webPolitica\node_modules\express\lib\router\index.js:277:22
at Function.process_params (C:\Quiz webPolitica\node_modules\express\lib\router\index.js:330:12)
at next (C:\Quiz webPolitica\node_modules\express\lib\router\index.js:271:10)
at serveStatic (C:\Quiz webPolitica\node_modules\express\node_modules\serve-static\index.js:75:16)
at Layer.handle [as handle_request] (C:\Quiz webPolitica\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (C:\Quiz webPolitica\node_modules\express\lib\router\index.js:312:13)
at C:\Quiz webPolitica\node_modules\express\lib\router\index.js:280:7
at Function.process_params (C:\Quiz webPolitica\node_modules\express\lib\router\index.js:330:12)
at next (C:\Quiz webPolitica\node_modules\express\lib\router\index.js:271:10)
at expressInit (C:\Quiz webPolitica\node_modules\express\lib\middleware\init.js:33:5)
at Layer.handle [as handle_request] (C:\Quiz webPolitica\node_modules\express\lib\router\layer.js:95:5) 

我已经尝试调试了几个小时并查找了其他人的问题,但我一无所获。非常感谢任何帮助。请在下面查看我的代码:

服务器.js

var express = require('express');
var app = express();

app.use(express.static(__dirname + '/public'));

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/iPolitico');

var Register_User_Schema = new mongoose.Schema({
    Firstname: { type: String, required: true },
    Lastname:  { type: String, required: true },
    Email:     { type: String, required: true, index: { unique: true } },
    Password:  { type: String, required: true }
}, {collection:"user"});

var Register_User = mongoose.model("Register_User", Register_User_Schema);

app.post('/register', function(req,res){
  new Register_User({
    Firstname: req.body.user_first_name,
    Lastname : req.body.user_last_name,
    Email    : req.body.user_email,
    Password : req.body.user_password
  }).save(function(err, doc){
    if(err){
      res.json;
    }else{
      app.use(express.static(__dirname + '/user_profile.html'));
    }
  })
});

app.get('/', function(req, res){
  //res.send('hello world');
  Register_User.find(function (err, data){
    res.json(data);
  });
});

app.listen(3000);

index.html

<form role="form" action="/register" method="POST" class="registration_form">  <!--action="javascript:;" onsubmit="register_user()"-->
        <div class="form-group">
    <label class="sr-only" for="form-first-name">First name</label>
            <input type="text" name="user_first_name"  id="user_first_name" placeholder="First name..." class="form-first-name form-control" >
         </div>
         <div class="form-group">
            <label class="sr-only" for="form-last-name">Last name</label>
            <input type="text" name="user_last_name" id="user_last_name" placeholder="Last name..." class="form-last-name form-control">
         </div>
         <div class="form-group">
            <label class="sr-only" for="form-email">Email</label>
            <input type="text" name="user_email" id="user_email" placeholder="Email..." class="form-email form-control" >
         </div>
         <div class="form-group">
            <label class="sr-only" for="form-password">Senha</label>
            <input type="password" name="user_password" id="user_password" placeholder="Senha..." class="form-password form-control" >
         </div>
         <div class="form-group">
             <label class="sr-only" for="form-password">Confirmar Senha</label>
            <input type="password" name="user_confirm_password" placeholder="Confirmar Senha..." class="form-password form-control" >
        </div>
        <button type="submit">Sign me up!</button>
</form>

作为附带问题。当我完成注册后,我想重定向到一个新页面。我目前有“app.use(express.static(__dirname + '/user_profile.html'));”,但我很确定这是不正确的或一半正确。

最佳答案

您的代码中没有body-parser

看看这是否可以帮助你:-

var express = require('express')
var bodyParser = require('body-parser')

var app = express()

app.use(bodyParser.urlencoded({ extended: false }))

app.use(bodyParser.json())

在顶部添加这个。

如果仍有问题,请引用 doc .

关于javascript - 类型错误 : Cannot read property 'user_first_name' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38021018/

相关文章:

javascript - 将对象插入数组

javascript - 限制给定 NodeJS 路由的某些 AngularJS 路由

Javascript 定时器在按键时重新启动?

node.js - MongoDB 聚合 - $lookup 性能

node.js - Jade : modify <head> from any place other than the main page

mongodb - db.setProfilingLevel(1, 10000) 返回错误 : "profile currently not supported via mongos"

mongodb - 如何使用正则表达式重命名 mongodb 字段

javascript - 无法以编程方式使用 Javascript 填充 iframe

javascript - $rootScope.$apply() 不工作

javascript - 从 Javascript 数组或类型化数组继承