最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

如何在nodejs中的html表中显示mysql查询结果?

运维笔记admin14浏览0评论

如何在nodejs中的html表中显示mysql查询结果?

如何在nodejs中的html表中显示mysql查询结果?

以下是Nodejs中查询结果的示例。

的NodeJS:

 [ RowDataPacket {
           id: 1,
booktitle: 'Leading',
bookid: '56353',
bookauthor: 'Sir Alex Ferguson' },
 RowDataPacket {
id: 2,
booktitle: 'How Google Works',
bookid: '73638',
bookauthor: 'Eric Smith' },
RowDataPacket {
id: 3,
booktitle: 'The Merchant of Venice',
bookid: '37364',
bookauthor: 'William Shakespeare' } ]

我需要以下面的html格式显示这些数据

booktitle               bookid         bookauthor
-----------------------------------------------------------
Leading                  56353          Sir Alex Ferguson
How Google Works         73638          Eric Smith
The Merchant of Venice   37364          William Shakespeare

我尝试使用以下代码分别获取密钥和值

 for(var i = 0; i < JSONObject.length; i++) {
 for(var val in JSONObject[i]) {
 console.log(JSONObject[i][val])
   }
}

但这并没有像预期的那样奏效。我的数据将是动态的。关键和价值将不断变化。所以我无法像JSONObject [i] .booktitle一样访问该值。我怎样才能获得一般解决方案?如何以表格格式显示数据。

回答如下:
  for(var i = 0; i < JSONObject.length; i++) {
     console.log("booktitle: " + JSONObject[i].booktitle)
     console.log("bookid: " + JSONObject[i].bookid)
     console.log("bookauthor: " + JSONObject[i].bookauthor)
  }
发布评论

评论列表(0)

  1. 暂无评论