使用node-postgres从SELECT返回结果

时间:2022-06-06 01:03:02

Having trouble getting results back from SELECT queries in node-postgres. Now rows are empty at the end of the codeblock. This select just returns a next value from a sequence in POSTGRESQL. I know that you can not retrieve results from callback, but are there anyone here who have used node-postgres(or any other database modules to node) that might know a fix?

无法从node-postgres中的SELECT查询中获取结果。现在,代码块末尾的行为空。此select只返回POSTGRESQL中序列的下一个值。我知道你无法从回调中检索结果,但是这里是否有人使用过node-postgres(或任何其他数据库模块到节点)可能知道修复?

client.connect();
var query = client.query("SELECT nextval(\'idgenerator\');");
var rows = [];
query.on('row', function(row, res) {
    rows.push(row);
});
query.on('end', function(result) {
    console.log(result.rowCount + ' rows were received');
});
//client.end();
console.log(rows);

2 个解决方案

#1


7  

You'll have to learn javascript / nodejs, and event programming.

你将不得不学习javascript / nodejs和事件编程。

query.on('row', function() { /*CODE*/ }) means : "when a row is read, execute CODE".

query.on('row',function(){/ * CODE * /})表示:“当读取一行时,执行CODE”。

This is asynchronous; so query.on() register the event, and returns.

这是异步的;所以query.on()注册事件,然后返回。

So when console.log(rows) is called, rows is still empty, because no 'row' event has been triggered on query, yet.

因此,当调用console.log(rows)时,行仍为空,因为在查询时尚未触发“row”事件。

You should try putting 'console.log(rows)' in the body of the query.on('end') event handler.

您应该尝试在query.on('end')事件处理程序的主体中放入'console.log(rows)'。

Everywhere in the code, also, you should write some console.log. You'll see the asynchronous thing.

在代码中的任何地方,你也应该编写一些console.log。你会看到异步的东西。

#2


4  

If we did not call the result.addRow() method, the rows array would be empty in the end event.

如果我们没有调用result.addRow()方法,则rows数组在结束事件中将为空。

var query = client.query("SELECT firstname, lastname FROM emps ORDER BY lastname, firstname");

query.on("row", function (row, result) {
    result.addRow(row);
});

query.on("end", function (result) {
    console.log(JSON.stringify(result.rows, null, "    "));
    client.end();
});

Ref: http://nodeexamples.com/2012/09/21/connecting-to-a-postgresql-database-from-node-js-using-the-pg-module/

参考:http://nodeexamples.com/2012/09/21/connecting-to-a-postgresql-database-from-node-js-using-the-pg-module/

#1


7  

You'll have to learn javascript / nodejs, and event programming.

你将不得不学习javascript / nodejs和事件编程。

query.on('row', function() { /*CODE*/ }) means : "when a row is read, execute CODE".

query.on('row',function(){/ * CODE * /})表示:“当读取一行时,执行CODE”。

This is asynchronous; so query.on() register the event, and returns.

这是异步的;所以query.on()注册事件,然后返回。

So when console.log(rows) is called, rows is still empty, because no 'row' event has been triggered on query, yet.

因此,当调用console.log(rows)时,行仍为空,因为在查询时尚未触发“row”事件。

You should try putting 'console.log(rows)' in the body of the query.on('end') event handler.

您应该尝试在query.on('end')事件处理程序的主体中放入'console.log(rows)'。

Everywhere in the code, also, you should write some console.log. You'll see the asynchronous thing.

在代码中的任何地方,你也应该编写一些console.log。你会看到异步的东西。

#2


4  

If we did not call the result.addRow() method, the rows array would be empty in the end event.

如果我们没有调用result.addRow()方法,则rows数组在结束事件中将为空。

var query = client.query("SELECT firstname, lastname FROM emps ORDER BY lastname, firstname");

query.on("row", function (row, result) {
    result.addRow(row);
});

query.on("end", function (result) {
    console.log(JSON.stringify(result.rows, null, "    "));
    client.end();
});

Ref: http://nodeexamples.com/2012/09/21/connecting-to-a-postgresql-database-from-node-js-using-the-pg-module/

参考:http://nodeexamples.com/2012/09/21/connecting-to-a-postgresql-database-from-node-js-using-the-pg-module/