Node. js operations on postgresql databases

Source: Internet
Author: User

1) return the query results to the client. Generally, the query results must be displayed. Otherwise, the query will be meaningless. In node. js, how does one return the queried data to the postgresql database? (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;} // After the query is executed, the result set is stored in results. You can use the console. log (results) to print it out. 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: // s: postgres @ localhost/my"; var client = new pg. client (conString); selectSQLString = 'select * from teacher '; client. connect (function (e Rror, 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> // use the callback function to obtain the select statement. the query result in the js file is: connection success... in select callback function {command: 'select', rowCount: 4, oid: NaN, rows: [{id: '1', name: 'aaa', pwd: '000000'}, {id: '2', name: 'bbb ', pwd: '000000'}, {id: '3', name: 'ccc', pwd: '000000'}, {id: '4', name: 'ddd ', pwd: '000000'}]} 2) normal database access Exit: Due to node. js features. If the connection is closed directly after the select function is called, the result may be different from what we expected: select. js unchanged, client. js: [javascript] <span style = "font-size: 14px;"> var select = require ('. /select '); var pg = require ('pg'); var conString = "tcp: // postgr Es: 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 ('conne Ction closed. \ n') ;}); </span> running result: connection success... connection closed. we can see that the connection is closed if the query result is not returned. This is because when node. js executes the query, it directly skips the execution of subsequent statements in a non-blocking manner. When the query is completed and the corresponding callback function is called. The correct processing method is client. js: [javascript] <span style = "font-size: 14px;"> var select = require ('. /select '); var pg = require ('pg'); var conString = "tcp: // s: 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;} co Nsole. log ('Connection success... \ n'); select. select (client, selectSQLString, function (result) {console. log (result); client. end (); console. log ('Connection closed. \ n') ;};}); </span> running result: connection success... in select callback function {command: 'select', rowCount: 4, oid: NaN, rows: [{id: '1', name: 'aaa', pwd: '000000'}, {id: '2', name: 'bbb ', pwd: '000000'}, {id: '3', name: 'ccc', pwd: '123' }, {Id: '4', name: 'ddd ', pwd: '000000'}]} Connection closed. 3) Return Value Problem: In most cases, the function has the return value 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), c Lient. 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: // s: 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> running result: 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: '000000'}, {id: '2', name: 'bbb ', pwd: '000000'}, {id: '3', name: 'ccc', pwd: '000000'}, {id: '4', name: 'ddd ', pwd: '000000'}]} Connection closed.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.