我有从数据库中检索数据集的代码:
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();
}
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();
}
这很好用,它会检索并将结果显示在终端上。
我想要实现的是将这些结果显示在html页面而不是控制台上,我已经开发了前端页面(使用jquery mobile)并且希望将数据库值显示在此页面而不是控制台。
任何人都可以指导我做正确的道路吗?例如,我知道div元素可以用来隐藏/显示数据,我只需要知道检索结果并将结果显示到html页面等元素上的最佳方法。
谢谢大家的帮助!
答案 0 :(得分:1)
类似的东西,但这只是谷歌的副本
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",
})
);