0

First I apologize for the typos, I'm Brazilian

I visited the question below, but it didn't help me solve my problem

ERR_HTTP_HEADERS_SENT: Cannot set headers after they are sent to the client

I have a method that I call from the route localhost:4000/returnvalues on the first request, it works as expected

but in the next one, it returns the following error

_http_outgoing.js:526
throw new ERR_HTTP_HEADERS_SENT('set'); ^

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

my code

module.exports = app => {

const formidable = require('formidable')
const path = require('path')
const folder = path.join("C:/Users/Rafael/Desktop", 'files')
const fs = require('fs')
var form = new formidable.IncomingForm({
    uploadDir: folder,
    keepExtensions: true
})

const returnvalues = async (req, res) => {
    form.parse(req, (_, fields, files) => {
        app.api.readFile.readFiles(files.Teste.path).then(values => {
            res.status(200).send(values)
           })
       })
   }

  return { returnvalues }
}

I tried unsuccessfully to use

return res.status(200).send(values)

this method returns me an object read from an excel spreadsheet

error from the server my code

terrymorse
  • 6,771
  • 1
  • 21
  • 27

1 Answers1

2

I've ran into the same error as you. If you put your form variable

var form = new formidable.IncomingForm({
    uploadDir: folder,
    keepExtensions: true
})

inside your returnvalues function, it should work.

ljc-dev
  • 51
  • 3