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

358
Visualizações
I am saving an IPFS file and using res.send() to push it to the front end, why is this not working?

The error tells me i "Cannot set headers after they are sent to the client"

async function saveFile(file) {
    await ipfs.add(file, (err, result) => {
        if (err){
            console.log(err);
        }
        console.log(result);
    });
}
app.post('/upload', async (req, res) => {
    const file = req.files.file;
    const fileName = req.body.fileName;
    const filePath = 'files/' + fileName;
    console.log(file);
    const fileHash = saveFile(JSON.stringify(file));
    res.send(fileHash);
    res.render('pages/upload.ejs', { fileName, fileHash });
    
});
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I'm having trouble finding the documentation of ipfs.add() in the js-ipfs documentation, so the code below is a guess.

When you use await with an async function, you don't generally provide a callback. The value will be returned by await, and errors are turned into exceptions, so I use try/catch around it.

saveFile() needs to return the file hash returned by ipfs.add(), and you need to use await when calling saveFile to get the returned value.

async function saveFile(file) {
  try {
    let result = await ipfs.add(file);
    console.log(result);
    return result;
  } catch (err) {
    console.log(err);
  }
}
app.post('/upload', async(req, res) => {
  const file = req.files.file;
  const fileName = req.body.fileName;
  const filePath = 'files/' + fileName;
  console.log(file);
  const fileHash = await saveFile(JSON.stringify(file));
  res.send(fileHash);
  res.render('pages/upload.ejs', {
    fileName,
    fileHash
  });
});

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