Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

681
Visualizações
How to save image url to mongodb and get it back

Please I am try to make post and get request from postman that will save the path of the image to mongodb not binary. I successfully posted the data to mongodb that includes form-data(text and file) but the file is a buffer. I want it to be a binary data, so that when I want to get it back I won't get a binary data. text is posting successfully, and am getting the required text back but image is sending binary and am getting binary back which is very bad idea.

Please I need help.

//POST Request
app.post('/request', upload.single('images'), async function(req, res){
    const requestBody = {
        name: req.body.name,
        description: req.body.description,
        images: req.file.buffer
    }
    const request = new Request(requestBody)

    try{
        await request.save()
        res.status(201).send()

    }catch(e){
        res.status(400).send(e)
    }

})

//GET request
app.get('/request', async function(req, res){

    try{
        const requests = await Request.find({})
        // console.log(requests)
        res.send()

    }catch(e){
        res.status(500).send()
    }
})
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You can save file path in public folder and save the path with string in mongodb

You need to create photos folder inside public folder and set it as static

mkdir -p public/photos

Then you can register path with express static

app.use(express.static(__dirname, 'public'))

and save the path which is required in db. Here you should use file's static path which is stored in req.file.path

const upload = require('multer')({ dest: path.join(__dirname, 'public/photos') })
app.post('/request', upload.single('images'), async function(req, res){
    const requestBody = {
        name: req.body.name,
        description: req.body.description,
        images: req.file.path
    }
    const request = new Request(requestBody)

    try{
        await request.save()
        res.status(201).send()

    }catch(e){
        res.status(400).send(e)
    }

})
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda