NodeJS Express应用程序上的错误“将标头发送到客户端后无法设置标头”

zlwx9yxi  于 2023-08-04  发布在  Node.js
关注(0)|答案(1)|浏览(132)

我确实有意返回了(因此userExists变量为True)。我遇到过这个错误很多次,但我不明白它是如何产生的
在日志中,我有两个错误,错误消息相同,但行不同
错误:* 错误[ERR_HTTP_HEADERS_SENT]:发送到客户端 * 行后无法设置标头:

return res.status(200).json({

字符串
错误:* 错误[ERR_HTTP_HEADERS_SENT]:发送到客户端 * 行后无法设置标头:

return res.status(500).json({


存储_用户代码:

const STORE_USER = async (req, res) => {
  try {
  
    const {
      name,
      surname,
      email,
      password,
      phone,
      country,
      city,
      address,
      zip,
      dob,
      gender,
    } = req.body;

    await prisma.$transaction(async (tx) => {
      const userExists = await tx.user.findFirst({
        where: {
          email,
        },
      });

      if (userExists) {  // userExists is True, so I guess it returned this
        return res.status(409).json({
          success: false,
          message: "User already exist",
          code: "B00001",
        });
      }

      const password_hashed = await bcrypt.hash(password, 10);

      const user = await tx.user.create({
        data: {
          name,
          surname,
          email,
          password: password_hashed,
          gender,
          phone,         
          address,
          zip,
        },
      });

       const token = crypto.randomBytes(30).toString("hex");

      await tx.verify_Account.create({
        data: {
          Users: {
            connect: {
              id: user.id,
            },
          },
          token,
        },
      });


    return res.status(200).json({
      success: true,
    });
  } catch (error) {
    console.log(error);
    return res.status(500).json({
      success: false,
      code: "A00010",
    });
  }
};


有人能解释一下这个错误是怎么遇到的吗?所以它发送了两个请求200和500,但是我不明白,因为我在userExist为True时返回
如果我在没有交易的情况下尝试,我不会遇到任何错误:因此,使用此代码:
我只是尝试没有棱镜交易的代码是好的,我没有得到标题错误。

const STORE_USER = async (req, res) => {
  try {
    const errors = validationResult(req);
    if (!errors.isEmpty()) {
      return res.status(400).json({ errors: errors.array(), success: false });
    }

    const {
      name,
      surname,
      email,
      password,
      phone,
      country,
      city,
      address,
      zip,
      dob,
      gender,
    } = req.body;

    const userExists = await prisma.user.findFirst({
      where: {
        email,
      },
    });

    if (userExists) {
      console.log("TEST1");
      return res.status(409).json({
        success: false,
        message: "User already exist",
        code: "B00001",
      });
    }

    console.log("TEST2");

    const clientRole = await prisma.role.findFirst({
      where: {
        name: "client",
      },
    });

    if (!clientRole) {
      console.log("Role not found");
      return res.status(500).json({
        success: false,
        code: "A00010",
      });
    }
    // TODO! CHECK IF COUNTRY AND CITY EXIST

    const date_date_of_birth = new Date(dob);

    const password_hashed = await bcrypt.hash(password, 10);

    const user = await prisma.user.create({
      data: {
        name,
        surname,
        email,
        password: password_hashed,
        gender,
        phone,
        country: {
          connect: {
            id: country,
          },
        },
        city: {
          connect: {
            id: city,
          },
        },
        address,
        zip,
        dob: date_date_of_birth,
        Roles: {
          connect: {
            id: clientRole.id,
          },
        },
      },
    });

    // generate token to pass to email
    const token = crypto.randomBytes(30).toString("hex");

    await prisma.verify_Account.create({
      data: {
        Users: {
          connect: {
            id: user.id,
          },
        },
        token,
      },
    });

    return res.status(200).json({
      success: true,
    });
  } catch (error) {
    console.log(error);
    return res.status(500).json({
      success: false,
      code: "A00010",
    });
  }
};

mzsu5hc0

mzsu5hc01#

当多次向客户端发送响应时会发生这种情况。例如,res.statusres.send

相关问题