简体   繁体   English

在Node.js中将检索到的SQL数据显示到HTML页面

[英]Display retrieved SQL data to HTML page in Node.js

I have code which retrieves dataset from our database: 我有从我们的数据库中检索数据集的代码:

 var pg = require('pg').native; // Format: "postgres://YourUserName:YourPassword@localhost:5432/YourDatabase"; var dbUrl = "url hidden"; function testConn(onDone) { pg.connect(dbUrl, function(err, client) { client.query("SELECT ad FROM alert WHERE ad = 132", function(err, result) { console.log("ad is: %d", result.rows.length); console.log("ad value is: %d", result.rows[0].ad_id ); onDone(); }); }); } // Let's call the function testConn(disconnectAll) // Disconnect all connections function disconnectAll() { pg.end(); } 

And this works just fine, it retrieves and displays the results onto the terminal. 这样就可以正常工作,它将检索结果并将其显示在终端上。

What I would like to achieve is to have these results appearing on a html page instead of the console, I've already developed the front-end page (used jquery mobile) and wish to have the database values come up on this page instead of the console. 我想要实现的是将这些结果显示在html页面而不是控制台上,我已经开发了前端页面(使用jquery mobile),希望在该页面上显示数据库值而不是控制台。

Can anyone please guide me on the right path on doing this? 谁能指导我正确的做法吗? For instance I know the div element can be used to hide/display data, I just need to know the best way to retrieve and display the results onto an element such as this onto a html page. 例如,我知道div元素可用于隐藏/显示数据,我只需要知道检索结果并将结果显示到html页面等元素上的最佳方法。

Thanks all help appreciated! 谢谢所有帮助!

something like this , but this is copy from google only 像这样的东西,但这只是从Google复制而来

  response.writeHead(200, {"Content-Type": "application/json"});
  var otherArray = ["item1", "item2"];
  var otherObject = { item1: "item1val", item2: "item2val" };
  response.write(
    JSON.stringify({ 
      anObject: otherObject, 
      anArray: otherArray, 
      another: "item",
    })
  );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM