Nodejs Postgres查询调用

时间:2017-02-09 03:19:28

标签: javascript node.js postgresql callback node-postgres

我正在尝试使用node-postgres创建者的建议,并将数据库调用作为单独的文件分开。这将启用各种样板查询类型的缩放和合并。不幸的是,brianc's explanation如何做到这一点并不适合我。 “cb(错误,结果)”抛出错误。

这是我的测试样板数据库调用(db.js):

            var pg = require('pg');
            var connectionString = 'postgres://postgres:password@10.10.10.10:5432/MyDatabase';
            var client = new pg.Client(connectionString, function(){
              if (err) {
                console.log(err);
                }
            });  
            var querytext = 'Select addr_type, addr_descr FROM ref_addr_type;';
            var result = [];
            var resultset = [];
            client.on('drain', client.end.bind(client));
            client.connect();

            module.exports = {
              query: function(callback) {

                var query = client.query(querytext, function(callback){

                  var resultsset = query.on('row', function(row, result) {
                    result.addRow(row);

                    query.on('end', function(res) {
                      client.end(function (err) {
                             if (err) throw err;

                             });    
                             console.log("JSON rows1: " + JSON.stringify(res));
                             return res; 
                    });
                    console.log("JSON rows2: " + JSON.stringify(result));   
                    return result;
                  });
                  console.log(resultset.length + ' rows were received');
                  callback = resultsset;
                  console.log("JSON3 rows: " + JSON.stringify(resultsset, null, ' '));
                  console.log("JSON4 rows: " + resultsset[0]);
                return callback;        
                })
              }
            }

这是调用它的脚本(call.js):

var db = require('../db');
var items = [];

items = db.query();

console.log("Main db: " + items);
console.log("Main db json: " + JSON.stringify(items));
console.log("Main db inspect: " + util.inspect(items));;

这是控制台输出:

0 rows were received
JSON3 rows: {
 "text": "Select addr_type, addr_descr FROM ref_addr_type;",
 "portal": "",
 "_result": {
  "command": "SELECT",
  "rowCount": 5,
  "oid": null,
  "rows": [
   {
    "addr_type": "2",
    "addr_descr": "Home"
   },
   {
    "addr_type": "3",
    "addr_descr": "Institution"
   },
   {
    "addr_type": "1",
    "addr_descr": "Company"
   },
   {
    "addr_type": "8",
    "addr_descr": "Organization"
   },
   {
    "addr_type": "10",
    "addr_descr": "Government"
   }
  ],
  "fields": [
   {
    "name": "addr_type",
    "tableID": 17549,
    "columnID": 1,
    "dataTypeID": 20,
    "dataTypeSize": 8,
    "dataTypeModifier": -1,
    "format": "text"
   },
   {
    "name": "addr_descr",
    "tableID": 17549,
    "columnID": 2,
    "dataTypeID": 1043,
    "dataTypeSize": -1,
    "dataTypeModifier": -1,
    "format": "text"
   }
  ],
  "_parsers": [
   null,
   null
  ],
  "rowAsArray": false
 },
 "isPreparedStatement": false,
 "_canceledDueToError": false,
 "_promise": null,
 "domain": null,
 "_events": {},
 "_eventsCount": 1
}
JSON4 rows: undefined

如您所见,call.js中没有console.log输出(“Main db:,Main db json:”和“Main db inspect:”)。此外,“结果集”中没有计算任何行,我无法从JSON4数组中获得任何结果。

我花了4天的时间来做这件事,可能会犯很多新手的错误。请提供一些有关获取db.js以将值传递给call.js的指导。

0 个答案:

没有答案