1)將查詢的結果返還給用戶端: 一般的查詢都需要將結果展示出來,否則查詢將顯得沒有意義。在node.js操作postgresql資料庫中,如何將查詢的資料返回回來呢? (1) select.js [javascript] <span style="font-size:14px;">function select(client,selectSQLString,<span style="color:#FF0000;">callback</span>) { client.query(selectSQLString, function selectCb(error, results) { console.log("in select callback function\n"); if (error) { console.log('GetData Error: ' + error.message), client.end(); return; } //在執行完查詢以後,結果集被存放在results中,你可以使用console.log(results)列印出來看看 if(results.rowCount > 0) { <span style="color:#FF0000;">callback(results);</span> } }); } exports.select = select; </span> (2) client.js[javascript] <span style="font-size:14px;">var select = require('./select'); var pg = require('pg'); var conString = "tcp://postgres:postgres@localhost/my"; var client = new pg.Client(conString); selectSQLString = 'select * from teacher'; client.connect(function(error, results) { if(error){ console.log('ClientConnectionReady Error: ' + error.message); client.end(); return; } console.log('connection success...\n'); select.select(client,selectSQLString,<span style="color:#FF0000;">function(result){ console.log(result); }</span>); });</span> //採用回呼函數的形式來擷取select.js檔案中的查詢結果 執行結果為: connection success... in select callback function { command: 'SELECT', rowCount: 4, oid: NaN, rows: [ { id: '1', name: 'aaa', pwd: '111' }, { id: '2', name: 'bbb', pwd: '222' }, { id: '3', name: 'ccc', pwd: '333' }, { id: '4', name: 'ddd', pwd: '444' } ] } 2) 訪問資料庫的正常退出: 由於node.js的特性,若直接在調用select函數之後就關閉串連,結果可能就和我們預想的不一樣了: select.js不變,client.js 如下: [javascript] <span style="font-size:14px;">var select = require('./select'); var pg = require('pg'); var conString = "tcp://postgres:postgres@localhost/my"; var client = new pg.Client(conString); selectSQLString = 'select * from teacher'; client.connect(function(error, results) { if(error){ console.log('ClientConnectionReady Error: ' + error.message); client.end(); return; } console.log('connection success...\n'); select.select(client,selectSQLString,function(result){ console.log(result); }); client.end(); console.log('Connection closed.\n'); }); </span> 運行結果: connection success... Connection closed. 可以看出,並沒有將查詢的結果返回回來,串連就關閉了。這是因為node.js執行到查詢的時候,採用非阻塞的方式,直接跳過執行後面的語句,當查詢執行完畢和調用相應的回呼函數。 正確的處理方式為:client.js: [javascript] <span style="font-size:14px;">var select = require('./select'); var pg = require('pg'); var conString = "tcp://postgres:postgres@localhost/my"; var client = new pg.Client(conString); selectSQLString = 'select * from teacher'; client.connect(function(error, results) { if(error){ console.log('ClientConnectionReady Error: ' + error.message); client.end(); return; } console.log('connection success...\n'); select.select(client,selectSQLString,function(result){ console.log(result); client.end(); console.log('Connection closed.\n'); }); }); </span> 運行結果: connection success... in select callback function { command: 'SELECT', rowCount: 4, oid: NaN, rows: [ { id: '1', name: 'aaa', pwd: '111' }, { id: '2', name: 'bbb', pwd: '222' }, { id: '3', name: 'ccc', pwd: '333' }, { id: '4', name: 'ddd', pwd: '444' } ] } Connection closed. 3) 傳回值的問題:大多數情況下,函數都有傳回值 select.js [javascript] <span style="font-size:14px;">function select(client,selectSQLString,callback) { var content = 'select beginning\n'; client.query(selectSQLString, function selectCb(error, results) { console.log("in select callback function"); if (error) { console.log('GetData Error: ' + error.message), client.end(); return; } if(results.rowCount > 0) { callback(results); } }); content += 'select end!\n'; return content; } exports.select = select; </span> client.js[javascript] <span style="font-size:14px;">var select = require('./select'); var pg = require('pg'); var conString = "tcp://postgres:postgres@localhost/my"; var client = new pg.Client(conString); selectSQLString = 'select * from teacher'; client.connect(function(error, results) { if(error){ console.log('ClientConnectionReady Error: ' + error.message); client.end(); return; } console.log('connection success...\n'); var content = select.select(client,selectSQLString,function(result){ console.log(result); client.end(); console.log('Connection closed.\n'); }); console.log(content); }); </span> 運行結果: connection success... select beginning select end! in select callback function { command: 'SELECT', rowCount: 4, oid: NaN, rows: www.2cto.com [ { id: '1', name: 'aaa', pwd: '111' }, { id: '2', name: 'bbb', pwd: '222' }, { id: '3', name: 'ccc', pwd: '333' }, { id: '4', name: 'ddd', pwd: '444' } ] } Connection closed.