gpt4 book ai didi

javascript - [ERR_HTTP_HEADERS_SENT] : Cannot set headers after they are sent to the client

转载 作者:搜寻专家 更新时间:2023-10-31 23:06:57 25 4
gpt4 key购买 nike

我正在使用 PostgreSQL 和 NodeJS 及其“PG 模块”。CRUD 有效,但有时在我保存或删除某些项目时不会自动更新 View 。这是我的代码,我认为错误就在这里,但我找不到它,我什么都试过了:'(

错误信息:

enter image description here

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 的异步行为而发生的。有时一个进程将处于事件循环中,我们发送响应,当它完成执行时,响应将再次发送。所以你可以使用回调或者async await来等待执行。

回调

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;

关于javascript - [ERR_HTTP_HEADERS_SENT] : Cannot set headers after they are sent to the client,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54301624/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com