gpt4 book ai didi

node.js - "cant set headers once they are sent"无缘无故,不使用 .next() Nodejs

转载 作者:行者123 更新时间:2023-12-03 22:43:47 26 4
gpt4 key购买 nike

我为此失去了头发!我不断收到“发送后无法设置 header ”错误,我确定我没有像在其他问题中看到的那样调用 Next()。我的代码一直在工作,直到我尝试进行一些重构,我没有改变这个类的任何东西,所以我不知道是什么导致了它。其他问题这么说是因为我尝试在调用 writeHead 后设置标题,但我只调用一次然后写入正文。

最糟糕的是它以前可以工作!这是代码

DataBase.prototype.execute = function execute(type, query, response, parameters) {  

this.sequelize = new Sequelize('inmobiliaria', 'root', 'root',
{
host: 'localhost',
port: 3306,
dialect: 'mysql'
}
);
//this.users.logIn(response.post['user'], response.post['pass']);



/*
from the sequelize docs:
Options is an object with the following keys:
{
logging: console.log, // a function (or false) for logging your queries
plain: false, // if plain is true, then sequelize will return all of the records within an array, otherwise it will return a single object/first record returned.
raw: true // Set this to true if you don't have a model definition for your query
}
*/
var options = { raw: true };

var result = this.sequelize.query(query, null, options, parameters)
.success(function (rows) {
if (type == 'select') {
response.writeHead(200, { 'Content-Type': 'application/json' });
response.write(parameters.callback + '(' + JSON.stringify(rows) + ')');//wrap the json inside the callback function to make a jsonp
response.end();
} else if (type == 'modify') {
response.writeHead(200, { 'Content-Type': 'text/html' });
response.write('Query was successful');
response.end();
}
}
).error(function (e) {
console.log('An error occured: ', e);
response.writeHead(404, { 'Content-Type': 'text/html' });
response.write('There was an error man, shits on fire yo ---> ' + e);
response.end();
}
);

}

错误出现在 response.writeHead() 的第一行
你可以看到我没有使用 .next() 或任何太花哨的东西,并且认为调用的顺序是正确的。目前没有其他类正在修改它,这是我查询数据库并写入结果以返回它的地方。我使用手动查询,因为我的数据库查询很复杂,我更喜欢使用 sql。

最佳答案

我发现了问题,在另一个类中,我有一些测试代码确实按照 Aaron 的建议再次编写了标题。猜猜深夜代码发挥了作用,无论如何感谢阅读。

关于node.js - "cant set headers once they are sent"无缘无故,不使用 .next() Nodejs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20986430/

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