Search code examples
javascriptnode.jsreactjsexpressmulter

multer I can't display my react app images


Hello my problem is that I use multer to store them on my server and their path on mongodb, but I can't get them to display on my react application.

thanks in advance for all the help.


Solution

  • You need to send the data from React in FormData, example:

    Node

    import multer from "multer" // const multer = require("multer")
    const upload = multer()
    // in your route
    app.post("/any-route", upload.array("name_id", 10), () => {
        let file = null
        if (req.files && req.files.lenght > 0){
         file = req.file[0]
        }
       // and this file you can send to MongoDB
    })
    

    In react you need use FormData

    const FormData = new FormData()
    FormData.append("name_id", file) // this file is the file you want send