DEV Community

How to upload an image using MERN stack

Yosra Skhiri on November 16, 2020

I'm currently learning the MERN stack by making a project, one of this project's functionalities is to create a user profile, by doing so I needed ...
Collapse
 
piyush0810 profile image
Piyush

In case we follow this for update functionality, how to delete the images stored. I can only change the name for new image and its reference. Also the mango db is not used I guess in this case.

Collapse
 
kaxiif profile image
Kashif Huusain

sure the image is not going to be stored in DB but the new unique name is going to be stored in database. Multer is storing the images in server folder... and you can delete images or any file by providing path and the name for that file can be access from database that you have stored when uploaded image

var fs = require('fs');
var filePath = 'c:/images/pic.png';
fs.unlinkSync(filePath);

Collapse
 
aniketrathi profile image
aniketrathi

req.file undefined

Collapse
 
2ndplayer profile image
Chris Camp

Make sure to add encType='multipart/form-data'

Collapse
 
jisooyu profile image
JS Yu

a few typos to fix :

  1. make the user.modal.js in the modals folder -->make the user.modal.js in the models folder
  2. in app.js of the backend ---> app.use(express.json({ extended: false })); mongoose.connect(uri, { useNewUrlParser: true, // useCreateIndex: true, useUnifiedTopology: true, });
  3. in App.js --> import User from './User';
  4. Make a routes folder and create user.js inside it. ---> Make a routes folder and create users.js inside it.
Collapse
 
kumarjayanth55 profile image
Jay

setNewAuthor is not defined .. Where is that useState defined actually?

Collapse
 
chioukhhmimi profile image
Chioukhhmimi

setNewAuthor = setNewUser

Collapse
 
kumarjayanth55 profile image
Jay

Thanks :)

Collapse
 
mukulrajpoot262610 profile image
Mukul Rajpoot

req.file undefined

Collapse
 
2ndplayer profile image
Chris Camp

Make sure to add encType='multipart/form-data'

Collapse
 
sanjana2109 profile image
Sanjana2109

I added this still getting the error and also I'm not able to upload any image

Collapse
 
jisooyu profile image
JS Yu

This is what I needed.Thanks a lot,Ms. Skhiri.

Collapse
 
tahoorahmed1 profile image
Tahoor Ahmed

req.file.filename show undefiend while post request in frontend . How can i solve this issue

Collapse
 
hellohowareu profile image
Şükürov Şükür

How could to upload multiple images