Node.js表单验证使无法读取未定义的属性“ profileimage”

时间:2019-04-03 07:38:35

标签: javascript node.js express

我正在按照教程尝试使用Express在Node.js中创建表单。我正在编写验证逻辑,如果该表单显示错误消息, 被提交为空,但是没有这样做,而是在第33行上给了我这个错误,我不知道为什么:

Cannot read property 'profileimage' of undefined

我调试了它,但不知道是什么原因造成的。 我在做错什么,该如何解决?

这是表格代码:

var express = require('express');
var router = express.Router();

/* GET users listing. */
router.get('/', function(req, res, next) {
  res.send('respond with a resource');
});

router.get('/register', function(req, res, next) {
  res.render('register', {
      'title': 'Register'
  });
});

router.get('/login', function(req, res, next) {
  res.render('login', {
      'title': 'Log In'
  });
});

router.post('/register', function(req, res, next) {
  var name = req.body.name;
  var email = req.body.email;
  var username = req.body.username;
  var password = req.body.password;
  var password2 =  req.body.password2;


  // Check for Image Field
  if(req.files.profileimage){
      console.log('uploading File...');

      // File Info
      var profileImageOriginalName = req.files.profileimage.originalname;
      var profileImageName = req.files.profileimage.name;

      var profileImageMime = req.files.profileimage.mimetype;
      var profileImagePath = req.files.profileimage.path;
      var profileImageExt = req.files.profileimage.extension;
      var profileImageSize = req.files.profileimage.size;
  } else {
      // Set a Default Image
      var profileImageName = 'noimage.png';
  }

    // Form Validation

    req.checkBody('name','Name field is required').notEmpty();
    req.checkBody('email','Email field is required').notEmpty();
    req.checkBody('email','Email not valid').isEmail();
    req.checkBody('username','Username field is required').notEmpty();
    req.checkBody('password','Password field is required').notEmpty();
    req.checkBody('password2','Password do not match').equals(req.body.password);

    // Check for errors
    var errors = req.validationErrors();

    if(errors){
        res.render('register', {
            errors: errors,
            name: name,
            email: email,
            username: username,
            password: password,
            password2: password2
        });
    } else {
        var newUser = new User({
            name: name,
            email: email,
            username: username,
            password: password,
            profileImage: profileImageName
        });

            // Create User
            User.createUser(newUser, function(err, user){
                if(err)throw err;
                console.log(user);
            });

            //Success Message
            req.flash('success', 'You are now registered and may log in');

            res.location('/');
            res.redirect('/');
    }
});

module.exports = router;

和我的app.js:

var express = require('express');
var path = require('path');
var logger = require('morgan');
var expressValidator = require('express-validator');
var cookieParser = require('cookie-parser');

var session = require('express-session');
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var bodyParser = require('body-parser');
var multer = require('multer');
var flash = require('connect-flash');
var mongo = require('mongodb');
var mongoose = require('mongoose');
var db = mongoose.connection;


var routes = require('./routes/index');
var users = require('./routes/users');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');


// Handle file uploads
var multer = require('multer');
var upload = multer({ dest: './uploads' });

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

// Handle Express Sessions
app.use(session({
    secret:'secret',
    saveUninitialized: true,
    resave: true
}));

// passport
app.use(passport.initialize());
app.use(passport.session());

// Validator
app.use(expressValidator({
  errorFormatter: function(param, msg, value) {
      var namespace = param.split('.')
      , root    = namespace.shift()
      , formParam = root;

    while(namespace.length) {
      formParam += '[' + namespace.shift() + ']';
    }
    return {
      param : formParam,
      msg   : msg,
      value : value
    };
  }
}));

app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));


app.use(flash());
app.use(function (req, res, next) {
  res.locals.messages = require('express-messages')(req, res);
  next();
});

app.get('*', function(req, res, next){
    res.locals.user = req.user || null;
    next();
});

app.use('/', routes);
app.use('/users', users);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
  app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
      message: err.message,
      error: err
    });
  });
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
  res.status(err.status || 500);
  res.render('error', {
    message: err.message,
    error: {}
  });
});


module.exports = app;

2 个答案:

答案 0 :(得分:1)

首先files应该是一个数组。如果确定只发送一个文件,请尝试以下代码块:

  // Check for Image Field
  if(req.files && req.files[0] && req.files[0].profileimage){
      console.log('uploading File...');

      // File Info
      var profileImageOriginalName = req.files[0].profileimage.originalname;
      var profileImageName = req.files[0].profileimage.name;

      var profileImageMime = req.files[0].profileimage.mimetype;
      var profileImagePath = req.files[0].profileimage.path;
      var profileImageExt = req.files[0].profileimage.extension;
      var profileImageSize = req.files[0].profileimage.size;
  } else {
      // Set a Default Image
      var profileImageName = 'noimage.png';
  }

答案 1 :(得分:0)

您的代码中包含以下行:

if(req.files.profileimage){

files未定义时,意味着req不具有属性files(也许不是根据该请求发送的文件?),然后尝试访问其中的任何内容引发错误(如果您不了解,则使节点崩溃)

因此,首先您应该像这样添加类型安全性:

if(req.files && req.files.profileimage) {

或更严格的说:

if(typeof req.files === 'object' && req.files.profileimage) {

(注意:只有在代码的另一部分尚未保证它是安全的(例如先前的类型安全测试,或者您在代码中自己明确分配了属性)时,才应该进行类型安全测试 >

我还看到您正在使用multer在req对象中获取文件对象。根据其文档,files可以是一个数组,也可以是字符串到数组的对象,请从“ multer” npm页面:

如果这样使用:

app.post('/photos/upload', upload.array('photos', 12), function (req, res, next) {

它将是一个数组:

  

req.files是photos个文件的数组

     

req.body将包含文本字段(如果有的话)

或者,如果这样使用:

var cpUpload = upload.fields([{ name: 'avatar', maxCount: 1 }, { name: 'gallery', maxCount: 8 }])
app.post('/cool-profile', cpUpload, function (req, res, next) {

然后它将是一个(字符串->数组)对象:

  

req.files是一个对象(字符串->数组),其中fieldname是键,而值是文件数组

     

例如

     

req.files ['avatar'] [0]->文件

     

req.files ['gallery']->数组

因此,您应该相应地更改代码。例如,如果选择第二种方法,则应更改为以下内容:

if(typeof req.files === 'object' && req.files.profileimage) {
  req.files.profileimage.forEach((profileimage) => {
    //do anything you previously did on req.files.profileimage, on profileimage
    let profileImageMime = profileimage.mimetype;
    //... more things and code...
  })
}