Anuja vinod
Anuja vinod

Reputation: 109

How to display mysql query result in html table in nodejs?

Following is an example of a query result in 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' } ]

I need this data to be displayed in the following html format

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

I tried to get the key and value separately using the following code

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

But this doesn't worked as expected. My data will be dynamic. The key and value will be keep on changing. So i can't access the value with JSONObject[i].booktitle like that. How can i get a general solution? How can i display the data in table format.

Upvotes: 1

Views: 1249

Answers (2)

user1197250
user1197250

Reputation: 33

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

Upvotes: 1

ahoang18
ahoang18

Reputation: 143

  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)
  }

Upvotes: 0

Related Questions