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

201
Views
Express 'response.download' returning non-standard data for the same request

Im generating a download file and every time i send request, returns something different for me, every time the file is generated as expected:

usuário,primeiro_nome,ultimo_nome,ativo,completou_o_treinamento
4,Foo,Bas,SIM,SIM
5,Ble,Loco,NÃO,NÃO
9,gui2,md,NÃO,NÃO
3137,foo,baz,NÃO,NÃO

But express returns is not the same, it should return the file in response.download but sometimes when i made the call, returns File not found other times return just the fields usuário,primeiro_nome,ultimo_nome,ativo,completou_o_treinamento, no body response and sometimes returns the file as should be, i don't know what is wrong because the file is alway generated in de right way

Generate file code

export const createReportFile = ({ reportLines, fromDate, toDate }: CreateReportProps): string => {
  const startDate = format(fromDate, 'dd-MM-yyyy')
  const endDate = format(toDate, 'dd-MM-yyyy')
  const aksReportDate = Date.now()

  const reportFileId = `${startDate}-to-${endDate}Report${aksReportDate}`

  const reportFileStream = fs.createWriteStream(
    path.join(__dirname, '..', '..', '..', 'tmp', 'uploads', `${reportFileId}.csv`),
  )

  const fieldsLine = 'usuário,primeiro_nome,ultimo_nome,ativo,completou_o_treinamento'

  reportFileStream.once('open', () => {
    reportFileStream.write(fieldsLine + '\r\n')
    reportLines.forEach(({ user_id, first_name, last_name, active, completed }) => {
      reportFileStream.write(`${user_id},${first_name},${last_name},${active},${completed}\r\n`)
    })
  })

  return reportFileId
}

Download file code


.
.
.
    const usersReport = ....generate users report


    const reportFileId = createReportFile({ fromDate, toDate, reportLines: usersReport })

    const filePath = path.join(__dirname, '..', '..', '..', 'tmp', 'uploads', `${reportFileId}.csv`)

    console.log(filePath)

    return response.download(filePath)
  }

Users report

[
  {
    user_id: 4,
    first_name: 'foo',
    last_name: 'baz',
    active: 'SIM',
    completed: 'SIM'
  },
  {
    user_id: 5,
    first_name: 'Ble',
    last_name: 'L',
    active: 'NÃO',
    completed: 'NÃO'
  },
  {
    user_id: 9,
    first_name: 'foooo',
    last_name: 'ma',
    active: 'NÃO',
    completed: 'NÃO'
  },
  {
    user_id: 3,
    first_name: 'foo',
    last_name: 'bar',
    active: 'NÃO',
    completed: 'NÃO'
  }
]
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Found the solution

I've change the generate file code to the code below

export const createReportFile = ({ reportLines, fromDate, toDate }: CreateReportProps): string => {
  const startDate = format(fromDate, 'dd-MM-yyyy')
  const endDate = format(toDate, 'dd-MM-yyyy')
  const aksReportDate = Date.now()

  const reportFileId = `${startDate}-to-${endDate}Report${aksReportDate}`

  let data = 'usuário,primeiro_nome,ultimo_nome,ativo,completou_o_treinamento\r\n'

  reportLines.forEach(({ user_id, first_name, last_name, active, completed }) => {
    data += `${user_id},${first_name},${last_name},${active},${completed}\r\n`
  })

  fs.writeFileSync(
    path.join(__dirname, '..', '..', '..', 'tmp', 'uploads', `${reportFileId}.csv`),
    data,
  )

  return reportFileId
}

Im not a expert to know why it works when i change but i believe is because in previous solution i was working with streams, and now we write file synchronously

If im wrong please correct me, i'll like to know more about this

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!