javascript - 从节点中的数据库返回多行
问题描述
我正在尝试返回产品的所有详细信息并以表格格式显示它们
我已经在我的 api 中尝试过这个
app.get('/test',(req,res) => {
const client = new Client({
connectionString: connectionString
})
client.connect()
client.query('select * from product',(err,res) =>{
console.log(err,res)
if(err){
console.log(err);
}else{
console.log(res);
}
client.end()
})})
我如何在节点中返回这个资源?
解决方案
您正在使用查询结果的内部资源将外部资源隐藏在路由处理程序中。
我必须对您的查询客户端做出一些假设,但如果内部 res 是行数组,只需将其传递给 res.json() 函数(另一个假设是您使用的是 Express)。
我的建议:
app.get('/test', (req, res) => {
const client = new Client({
connectionString: connectionString
})
client.connect()
client.query('select * from product', (err, rows) => {
console.log(err, rows)
if (err) {
console.log(err);
} else {
console.log(rows);
res.json(rows);
}
client.end()
})
});
推荐阅读
- android - 不能在aidl中使用自定义类
- sql-server - 更新触发器不会触发两个语句
- arrays - 在nodejs中读取数组内的对象
- c# - 为什么在 C# 中使用 ReadWriteLock 时出现文件共享异常?
- r - 如何从其他变量的级别在 R 中创建新变量
- java - 找不到“org.springframework.http.codec.ServerCodecConfigurer”类型的 bean
- python - pip3 客户端错误 url 需要 SSL
- ios - iOS UserDefaults 落后于保存的内容
- c# - 安装 netstandard 2.0 NuGet 包:无法加载文件或程序集 System.Runtime.Extensions
- r - 如何以另一个名称安装 R 包?