2017-10-13 2 views
0

Also ich versuche, Benutzer zu ermöglichen, ein Profil-Bild für dort Profil zu laden und ich habe die Form ein bisschen wie ich es will jetzt aber ich halte läuft in einen Fehler zu sagen TypeError: Cannot read property 'profilePicUpload' of undefinedExpress-Fileupload Bild hochladen funktioniert nicht

obwohl ich fast sicher bin, dass alle meine Code in Ordnung ist hier die 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('/'); 
    } 

und hier ist mein Upload-Formular

  <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> 

, wenn ich es jus Eingabetaste drücken t zeigt diesen Fehler, den ich oben gesagt habe? kann mir bitte jemand helfen

Antwort

0

Den Inhalt von req.files ausdrucken. dh console.log(req.files); sollten Sie stattdessen req.files.profilePictureToUpload verwenden? Im Gegensatz zu req.files.profilePicUpload;

Wenn die Eigenschaft nicht angezeigt wird, liegt wahrscheinlich ein Problem vor, dass die Dateiinhalte nicht ordnungsgemäß an das Anforderungsobjekt angehängt werden.

Außerdem müssen Sie den Dateiupload nicht global auf jede einzelne Route anwenden, die Sie verwenden. Verwenden Sie es einfach für den Endpunkt, der tatsächlich hochgeladen wird:

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