express-fileupload图片上传无效

时间:2017-10-13 14:56:39

标签: javascript node.js express file-upload

所以我试图允许用户上传个人资料的个人资料图片,我得到的表格有点我现在想要的但我一直遇到错误说TypeError: Cannot read property 'profilePicUpload' of undefined

尽管我几乎可以肯定我的所有代码都很好,但这里是routes.js

    // app/routes.js
    var mysql = require('mysql');
    var dbconfig = require('../config/database');
    var connection = mysql.createConnection(dbconfig.connection);
    const fileUpload = require('express-fileupload');

    module.exports = function(app, passport) {

        app.get('/logout', function(req, res) {
            req.logout();
            res.redirect('/');
        });
        // =====================================
        // HOME PAGE (with login links) ========
        // =====================================
        app.get('/', function(req, res) {
            res.render('login.ejs', { message: req.flash('loginMessage') }); // load the index.ejs file
        });

        // =====================================
        // LOGIN ===============================
        // =====================================
        // show the login form
        app.get('/login', function(req, res) {

            // render the page and pass in any flash data if it exists
            res.render('login.ejs', { message: req.flash('loginMessage') });
        });


        app.use(fileUpload());

       app.post('/upload', function(req, res) {
         console.log(req.files.profilePicUpload);
       });



        passport.serializeUser(function(user, done) {
            done(null, user.id);
        });


        passport.deserializeUser(function(id, done) {
            User.findById(id, function(err, user) {
                done(err, user);
            });
        });
        // process the login form
        app.post('/login', passport.authenticate('local-login', {
                successRedirect: '/mainchat', // redirect to the secure profile section
                failureRedirect: '/login', // redirect back to the signup page if there is an error
                failureFlash: true // allow flash messages
            }),
            function(req, res) {
                console.log("hello");

                if (req.body.remember) {
                    req.session.cookie.maxAge = 1000 * 60 * 3;
                } else {
                    req.session.cookie.expires = false;
                }
                res.redirect('/');
            });

        // =====================================
        // SIGNUP ==============================
        // =====================================
        // show the signup form
        app.get('/signup', function(req, res) {
            // render the page and pass in any flash data if it exists
            res.render('signup.ejs', { message: req.flash('signupMessage') });
        });

        // process the signup form
        app.post('/signup', passport.authenticate('local-signup', {
            successRedirect: '/mainchat', // redirect to the secure profile section
            failureRedirect: '/signup', // redirect back to the signup page if there is an error
            failureFlash: true // allow flash messages
        }));

        // =====================================
        // PROFILE SECTION =========================
        // =====================================
        // we will want this protected so you have to be logged in to visit
        // we will use route middleware to verify this (the isLoggedIn function)
        app.get('/profile', isLoggedIn, function(req, res) {
            var aboutUser = connection.query("SELECT about FROM users WHERE username = ?", req.user, function(err, rows) {
                res.render('profile.ejs', {
                    user: req.user,
                    about: rows
                });
            });
        });

        app.get('/mainchat', isLoggedIn, function(req, res) {
            var username = req.user.displayName;
            res.render('mainchat.ejs', username);
            console.log(req.user.displayName)
        });
        // =====================================
        // LOGOUT ============================= rows.forEach(function(row) {=
        // =====================================
        app.get('/logout', function(req, res) {
            req.logout();
            res.redirect('/');
        });



    };

    // route middleware to make sure
    function isLoggedIn(req, res, next) {

        // if user is authenticated in the session, carry on
        if (req.isAuthenticated())
            return next();

        // if they aren't redirect them to the home page
        res.redirect('/');
    }

这是我的上传表单

            <form id="form" action="/upload" method="POST">
                <div class="fileUpload" id="profile-picture-upload">

                    <input name="profilePictureToUpload" type="file" id="profile-picture-upload" class="upload" />
                </div>
                <input type="submit" name="profilePicUpload" id="upload-profilePic-button"></input>
            </form>

当我按下输入它只是显示我上面说的错误?有人可以帮帮我

1 个答案:

答案 0 :(得分:1)

打印出req.files的内容。即console.log(req.files); 你应该使用req.files.profilePictureToUpload吗?与req.files.profilePicUpload;

相反

如果您没有看到该属性,则可能是文件内容未正确附加到请求对象上的问题。

此外,您无需将文件上传全局应用于您正在使用的每个路由。只需将它用于实际上传的端点:

   app.post('/upload', fileUpload(), function(req, res) {
     console.log(req.files.profilePicUpload);
   });