S
S
Shakir Darion2021-02-26 04:19:12
Node.js
Shakir Darion, 2021-02-26 04:19:12

ERR_HTTP_HEADERS_SENT: Cannot set cookie?

this is my code

import { Request, Response } from "express";
import { database } from "firebase-admin";

async function updatereAccessToken(
  req: Request,
  res: Response,
  db: database.Database
) {
  try {

    await db
      .ref(
        `/token/${"-MUQK0wGmrjPVBdFH7gr"}/${"90417abf-b085-4072-b4ec-f009b5fe4016"}`
      )
      .set("90417abf-b085-4072-b4ec-f009b5fe4016");

    
    await db
      .ref(
        `/token/${"-MUQK0wGmrjPVBdFH7gr"}/${"90417abf-b085-4072-b4ec-f009b5fe4016"}`
      )
      .set("90417abf-b085-4072-b4ec-f009b5fe4016");

//comments

    res.cookie("act", "tokenAuth.accessToken", {
      httpOnly: true,
      sameSite: "lax",
    });

    res.cookie("ret", "tokenAuth.refreshToken", {
      httpOnly: true,
      sameSite: "lax",
    });

    return "currentUser";
  } catch (error) {}
}


The code works before the comment. Then I get an error

[ERR_HTTP_HEADERS_SENT]: невозможно установить заголовки после их отправки клиенту на ServerResponse.setHeader (_http_outgoing.js: 558: 11)


Divide the code into two parts

1
await db
      .ref(
        `/token/${"-MUQK0wGmrjPVBdFH7gr"}/${"90417abf-b085-4072-b4ec-f009b5fe4016"}`
      )
      .set("90417abf-b085-4072-b4ec-f009b5fe4016");

    
    await db
      .ref(
        `/token/${"-MUQK0wGmrjPVBdFH7gr"}/${"90417abf-b085-4072-b4ec-f009b5fe4016"}`
      )
      .set("90417abf-b085-4072-b4ec-f009b5fe4016");


2
res.cookie("act", "tokenAuth.accessToken", {
          httpOnly: true,
          sameSite: "lax",
        });
    
        res.cookie("ret", "tokenAuth.refreshToken", {
          httpOnly: true,
          sameSite: "lax",
        });


These two pieces of code work separately, but not together. What is the reason ?.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question