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] & lt; spanstyle & quot; font-s... syntax 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] function select (client, selectSQLString, callback) {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) is printed to see if (results. rowCount> 0) {callback (results) ;}}) ;} exports. sele Ct = select; (2) client. js [javascript] 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 su Ccess... \ n'); select. select (client, selectSQLString, function (result) {console. log (result) ;}); // 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) number of workers Normal exit of the database: Due to node. js features, if the connection is closed directly after the select function is called, the result may be me ?? The expectation is different: select. js unchanged, client. js: [javascript] 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 ('conne Ction success... \ n'); select. select (client, selectSQLString, function (result) {console. log (result) ;}); client. end (); console. log ('Connection closed. \ n') ;}); 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] 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'); select. select (client, selectSQLString, function (result) {console. log (result); client. end (); console. log ('Connection closed. \ n') ;};}); 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: '20140901'}, {id: '4', name: 'ddd ', pwd: '20140901' }]} Connection closed. 3) Return Value Problem: In most cases, the function has the return value select. js [javascript] 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; client. js [javascript] 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) ;}); 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.

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.