[err_http_headers_sent]。在向客户端发送头信息后不能设置头信息[英] [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

本文是小编为大家收集整理的关于[err_http_headers_sent]。在向客户端发送头信息后不能设置头信息的处理方法,想解了[err_http_headers_sent]。在向客户端发送头信息后不能设置头信息的问题怎么解决?[err_http_headers_sent]。在向客户端发送头信息后不能设置头信息问题的解决办法?那么可以参考本文帮助大家快速定位并解决问题。

问题描述

我正在使用 PostgreSQL 和 NodeJS 及其"PG 模块".CRUD 有效,但有时在我保存或删除某些项目时不会自动更新视图.这是我的代码,我认为错误就在这里,但我找不到它,我尝试了一切:'(

错误信息:

在此处输入图片描述

const controller = {};
const { Pool } = require('pg');

var connectionString = 'postgres://me:system@localhost/recipebookdb';
const pool = new Pool({
    connectionString: connectionString,
})

controller.list = (request, response) => {
    pool.query('SELECT * FROM recipes', (err, result) => {
        if (err) {
            return next(err);
        }
           return response.render('recipes', { data: result.rows });
    });
};

controller.save = (req, res) => {
    pool.query('INSERT INTO recipes(name, ingredients, directions) VALUES ($1, $2, $3)',
        [req.body.name, req.body.ingredients, req.body.directions]);
    return res.redirect('/');
};

controller.delete = (req, res) => {
    pool.query('DELETE FROM RECIPES WHERE ID = $1', [req.params.id]);
    return res.redirect('/');
}

module.exports = controller;

PD:CRUD 有效,但有时会出现该错误.

推荐答案

之前发送过响应,然后再次尝试发送响应时会出现此错误.为此,您必须检查是否有任何代码将您的响应发送两次.有时它是由于 nodejs 的异步行为而发生的.有时一个进程将处于事件循环中,我们发送响应,当它完成执行时,将再次发送响应.所以你可以使用回调或异步等待来等待执行.

回调

const controller = {};
const { Pool } = require('pg');

var connectionString = 'postgres://me:system@localhost/recipebookdb';
const pool = new Pool({
    connectionString: connectionString,
})

controller.list = (request, response) => {
    pool.query('SELECT * FROM recipes', (err, result) => {
        if (err) {
            return next(err);
        }
           return response.render('recipes', { data: result.rows });
    });
};

controller.save = (req, res) => {
    pool.query('INSERT INTO recipes(name, ingredients, directions) VALUES ($1, $2,$3)',
        [req.body.name, req.body.ingredients, req.body.directions],function(err,resp) 
       {
         if(err){
          console.log(err)
      }else{
          return res.redirect('/');
      }
       });
};

controller.delete = (req, res) => {
    pool.query('DELETE FROM RECIPES WHERE ID = $1',  [req.params.id],function(err,resp){
     if(err){
          console.log(err)
      }else{
          return res.redirect('/');
      }
 });
}

module.exports = controller;

或者你也可以使用 async await 等待执行,然后发送响应.

异步/等待

const controller = {};
const { Pool } = require('pg');

var connectionString = 'postgres://me:system@localhost/recipebookdb';
    const pool = new Pool({
    connectionString: connectionString,
})

controller.list = async(request, response) => {
   try{
       const result = await pool.query('SELECT * FROM recipes');
       return response.render('recipes', { data: result.rows });
   }
    catch(err){
       return next(err);
   }
};

controller.save = async(req, res) => {
    try{
       await pool.query('INSERT INTO recipes(name, ingredients, directions) VALUES ($1, $2,$3)',[req.body.name, req.body.ingredients, req.body.directions]);
       return res.redirect('/');
   }
    catch(err){
       return next(err);
   }
};

controller.delete = async(req, res) => {
    try{
        await pool.query('DELETE FROM RECIPES WHERE ID = $1', [req.params.id]);
        return res.redirect('/');
    }catch(err){
       console.log(err);
    }
}

module.exports = controller;

本文地址:https://www.itbaoku.cn/post/1763897.html