1

I am not able to access req.file in my put request. I am trying to upload a image from the form together with the other inputs to an collection in MongoDB. I am able to change the variables in the collection, except for uploading an image. Have tried different examples but haven't been able to make it work. Have tried different variants of creating the upload variable with multer, but I can't make it work.

This is what i have in the ejs file:

<form action="/profile/edit?_method=PUT"  method="POST">
          <h5 class="grey-text text-darken-3">Change your profile:</h5>
          <div class="input-field">
            <label htmlFor="firstName">Firstname:</label>
            <input type="text" id='firstName' name="firstName" value="<%= user.firstName %>" required/>
          </div>
          <div class="input-field">
              <label htmlFor="lastName">Lastname: </label>
              <input type="text" id='lastName' name="lastName" value="<%= user.lastName %>"" required/>
          </div>
          <label htmlFor="gender">Gender:</label>
          <% let def = ""; let m = ""; let w = ""; let o= "";
              if(user.gender == "Male"){m="selected";} 
              else if(user.gender== "Female"){w="selected";}
              else if(user.gender== "Other"){o="selected";}
              else{def="selected";}
          %>
          <div class="input-field col s12">
            <select id="gender" name="gender" value="Women">
              <option  value="" disabled  <%= def %> >Choose your option</option>
              <option value="Male"   <%= m %> >Male</option>
              <option value="Female" <%= w %>>Female</option>
              <option value="Other" <%= o %>>Other</option>
            </select>
          </div>
          <% var theDate = "2015-08-09";
            console.log(theDate);
            console.log(user);
            if(user.birthdate != ""){
              theDate= user.birthdate;
              console.log(theDate);
            }
            console.log("THEDATE: ");
            console.log(theDate);
          %>
          <div class="input-field">
            <label htmlFor="birthdate">Birthday</label>
            <input type="date" data-date="" id="birthdate" name="birthdate" data-date-format="DD MMMM YYYY" value="<%= theDate %>" onChange={this.handleChange}/>
          </div>
          <label for="about">About me:</label>
          <textarea id="about" name="about" ><%= user.about %></textarea>

          <div>
            <label>Cover</label>
            <input type="file" name="profilepicture" id="profilepicture" value=null>
          </div>
          
          <input type="hidden" name="id" value= "<%= user._id %>"/>
          <div class="input-field">
            <button class="btn pink lighten-1">Change Profile</button>
          </div>
        </form>

This is my router file:

const express= require('express');
const router= express.Router();
const profileController = require('../controllers/profileController');

var multer = require("multer")
var upload = multer({ dest: '/edit' });

router.get('/', profileController.profile_my);
router.get('/edit', profileController.profile_edit_get);
router.put('/edit', upload.single('profilepicture'), profileController.profile_edit_put);

This is in my controller file:

const profile_edit_put = (req,res) =>{
    console.log(req.file);

    User.findByIdAndUpdate({_id: req.body.id}, {
        $set:{
            firstName: req.body.firstName,
            lastName: req.body.lastName,
            gender: req.body.gender,
            about: req.body.about,
            birthdate: req.body.birthdate,
        }
    })
    .then(result =>{
        console.log(" redirect app");
        res.redirect('/profile');
    })
    .catch(err=>{ 
        console.log(err);
    });
}

Would be grateful for some help, I feel a bit lost. Thanks in advance.

0 Answers0