猫鼬模式的属性未定义

时间:2019-12-28 05:17:16

标签: javascript node.js mongodb express mongoose

尝试使用我的应用创建新的用户帐户时,出现此错误。

TypeError: Cannot read property 'firstName' of undefined

这是我的models.js文件

mongoose.model('User', new mongoose.Schema({
        firstName: String,
        lastName: String,
        email: String,
        passwordHash: String,
        subscriptionActive: {type: Boolean, default: false},
        customerId: String,
        subscriptionId: String,
})); 

这是我的前端代码。

<form class="user" action="/signup" method="post">
                <div class="form-group row">
                  <div class="col-sm-6 mb-3 mb-sm-0">
                    <input type="text" class="form-control form-control-user" required name="firstName" placeholder="First Name">
                  </div>
                  <div class="col-sm-6">
                    <input type="text" class="form-control form-control-user" required name="lastName" placeholder="Last Name">
                  </div>
                </div>
                <div class="form-group">
                  <input class="form-control form-control-user" required type="email" name="email" placeholder="Email"/>
                </div>
                <div class="form-group row">
                  <div class="col-sm-6 mb-3 mb-sm-0">
                    <input class="form-control form-control-user" required type="Password" name="password" placeholder="password"/>
                  </div>

这些是我的应用程序代码中的相关内容:

var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var mongoose = require('mongoose');
require('./models.js');
var bcrypt = require('bcryptjs');
var expressSession = require('express-session');
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var dotenv = require('dotenv');
dotenv.config();
const bodyParser = require('body-parser');

var User = mongoose.model('User');
mongoose.connect('{confidential}', { useNewUrlParser: true, useUnifiedTopology: true  });

// Middleware
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(expressSession({
  secret: process.env.EXPRESS_SESSION_SECRET,
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

passport.use("signup-local", new LocalStrategy({
  usernameField: "email",
  passwordField: "password"
}, function(req, email, password, next) {
  console.log('req :',req, 'req.body :', req.body);
  User.findOne({
    email: email
  }, function(err, user)  {
    if (err) return next(err);
    if (user) return next({message: "User already exists"});

    let newUser = new User({
      firstName: req.body.firstName,
      lastName: req.body.lastName, 
      email: email,
      passwordHash: bcrypt.hashSync(password, 10), // Could potentially be hashed asynchronously
    });
    // If newUser is passed successfully redirect to the main page
    newUser.save(function(err) {
      next(err, newUser);
    });
  });

}));

app.post('/signup', passport.authenticate('signup-local', { failureRedirect: '/' }),
    function(req, res) {
      res.redirect('/main');
    });

我正在尝试创建一个新用户,同时还要通过Passport.js进行身份验证。 日志显示req.body未定义。我不明白为什么会这样。有人可以解释吗?

0 个答案:

没有答案