Express app.param issue with res.send
Express app.param issue with res.send
我正在尝试发送回复,但我不确定如何做到这一点。当我尝试运行"res.send or res.response"这段代码时,我Can't set headers after they are sent
收到此错误。
我创建路由的方式是,如果输入了/id,那么我将执行 SQL 查询以检查 TableA 中是否存在该 id。如果 id 存在,那么我将获得该项目,例如。食物,在与该 ID 对应的table_type列中。然后我将转到名为表的table_type项,例如。食物,我将从该表中获取属性并发送响应或呈现页面。
// parameter middleware that will run before the next routes
app.param('num', function(req, res, next, num) {
var array = [];
var id;
var table;
client.connect(function(err) {
if(err) {
return console.error('could not connect to postgres', err);
}
client.query("SELECT id, table_type FROM TableA WHERE id=" + num, function(err, result1) {
if(err) {
return console.error('error running query', err);
}
else {
//console.log("###### THIS IS IT!!! #######");
//console.log(result1);
id = result1.rows[0].id;
table = result1.rows[0].table_type.trim();
var latitude;
var longitude;
if (table === "Food") {
var name;
var description;
client.query("SELECT name, description, quantity, color FROM FoodTable WHERE item_id=" + id, function(err, result2) {
if(err) {
return console.error('error running query', err);
}
else {
name = result2.rows[0].name.trim();
description = result2.rows[0].description.trim();
quantity = result2.rows[0].quantity.trim();
color = result2.rows[0].color.trim();
array.push(name, description, quantity, color);
console.log(array);
//---- WHY NO WORK? ------
res.send(array);
}
client.end();
});
}
}
client.end();
});
});
next();
});
代码的措辞解释:我正在尝试编写路由,通过调用 SQL 查询client.query
来使用 app.param 检查 TableA 中是否存在 id。我不能在函数外部使用变量,因为一旦函数调用完成,就不会引用数据。所以我连接了我的查询。因此,一旦 id 存在于 TableA 中,我就会将id
和table_type
存储到一个变量中,并在 if 语句中检查类型。因此,如果表类型是饮料,那么我将查看饮料表以获取与 id 关联的属性。
你在异步数据库调用之外调用 next()。因此,发生的情况是 next 已经执行并返回了对请求的响应,因此在调用 res.send 方法时已经设置了标头。
相关文章:
- 如何将async.whilst调用的结果发送到和express.js res.send()
- 执行返回res.send()的Javascript代码
- 如何在ExpressJS生产中捕获res.send(404)
- Express.js 中 res.send 和 res.json 之间的区别
- Express app.param issue with res.send
- 将 res.send() 移到函数之外
- Express, AJAX - res.send 不发送任何数据
- NodeJS-将命令行输出存储为var,并通过res.send()将其返回到客户端
- Express(node.js)-res.send只发送部分响应
- ExpressJS/Mongoose res.send()没有'不起作用
- 如何在Node.js中使用res.send()
- res.send vs res.locals in nodejs
- Node . js检查mysql表中是否存在数据.—TypeError: res.send不是函数
- 页面在res.send()之后没有上传
- 无法使用express with node使用res.send()发送数字
- Node.js,Express:如何使用客户端处理res.send 值
- res.send and res.render calls
- 如何在“res.send”之后但在响应离开服务器之前挂钩,==>当res.statusCode知道时
- 为什么节点js的res.send不工作
- node-express error: express deprecated res.send(status):使用re