[英]Unable to send object in Express response
I'm trying to send all the data from my database back using res.send, but it's throwing me an error. 我正在尝试使用res.send从我的数据库发回所有数据,但它给我一个错误。 On my frontend, I'm calling the endpoint
select
and trying to return all rows and data within the database. 在我的前端,我正在调用端点
select
并尝试返回数据库中的所有行和数据。
app.get('/select', function (req, res) {
con.query('SELECT * FROM Contracts', function (err, rows, fields) {
if (err) {
console.log(`Error: ${err}`);
}
for (var i = 0; i < rows.length; i++) {
var row = rows[i];
res.send(row);
}
})
});
You should use res.write
instead of res.send
as the latter terminates the function (nothing after the first use of res.send
is executed) 您应该使用
res.write
而不是res.send
因为后者终止该函数(在第一次使用res.send
没有执行任何操作)
app.get('/select', function (req, res) {
con.query('SELECT * FROM Contracts', function (err, rows, fields) {
if (err) {
console.log(`Error: ${err}`);
}
for (var i = 0; i < rows.length; i++) {
var row = rows[i];
res.write(row);
}
res.end(); //this finally sends the response
})
});
Or if you want to use res.send
send the whole rows
array: 或者如果你想使用
res.send
发送整rows
数组:
app.get('/select', function (req, res) {
con.query('SELECT * FROM Contracts', function (err, rows, fields) {
if (err) {
console.log(`Error: ${err}`);
}
res.send(rows);
})
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.