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

352
Visualizações
Cannot set headers after they are sent to the client when adding data to firebase

I've been having this error

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client 
at ServerResponse.setHeader (_http_outgoing.js:533:11)

After I added an update firebase code in a socket.io

io.on('connection', socket => { 
... 

socket.once('myEvent', async (dataToSend) => {
 try {
    await db
    .collection('myCollection')
    .doc('myDoc')
    .update({keyToUpdate: dataToSend})
    } catch (err) {
       console.log(err)
    }
  }
}

I thought the problem was with socket so I did the same logic but with a POST route outside the socket, and got the same error, I tried adding a return but didn't work

I suspect it could be because this function is a promise and I'm not handling asynchronous behaviour well, or because the server is acting as a stream thanks to socket.io and this firebase function doesn't like that.

I have other firebase functions in the code but they are get requests (.onSnapshot()) the error only happens with data adding firebase functions (.set(), .add(), .update())

Would appreciate some help here, thanks.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Fixed, didn't exactly knew how onSnapshot works, so everytime an update was made it triggered its callback which was sending info and triggering the error.

Changed to .get().then() and that fixed it

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