Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

154
Views
async/await and image upload

I want to create a post and upload an image if it is provided.

If I do upload the image everything works fine but if I don't I get this error:

UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'image' of null

I understand it's to do with async/await but I do not know how to rewrite it or that the problem is specifically?

app.post("/posts/create", async (req, res) => {
    let image = req.files.image; 

    if (image !== null) {
        image.mv(
            path.resolve(__dirname, "public/images", image.name) 
        );
    }

    await Post.create(req.body);
    res.redirect("/");
});
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

That's not because async/await, it is simply because you don't have any .files in your request, so you just need a middleware for handling multipart/form-data like multer, and then pass the middleware through the app.post so you can access to req.files, if you want to upload that image to a server like cloudinary you can just use multer-cloudinary-storage.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!