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

205
Visualizações
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 Respostas
Responde à pergunta

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 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