W
W
websjunior2017-02-15 15:30:02
PostgreSQL
websjunior, 2017-02-15 15:30:02

Why can't connect to PostgreSQL on node js?

There are controllers in a separate file, the post should display data from the database. displays order-1 and order-2 as needed, but from the database there is an error in the console.

/* POST home page. */
router.post('/', function(req, res, next) {
  if (req.body.action === "order-1")
    res.send("order-1");
  else if(req.body.action === "order-2")
    res.send("order-2");

  //connect
  pg.connect(connect, function(err, client, done) {
    if(err) {
      return console.log('error fetching client from pool', err);
    }
    client.query('SELECT * FROM orders', function(err, result) {
      if(err) {
        return console.error('error running query', err);
      }
      console.log(result.rows['name']);
      done();
    });
  });
});

And here is the main file in which the path to the database
var pg = require('pg');

//DB connect
var connect = "postgres://andrey:[email protected]:5432/bytik-life";

var index = require('./routes/index');

(I also attached the connection of routers. There are post and get requests in them.)
So, for writing syntax errors 0, but writes out the error
"
Error: Can't set headers after they are sent.
    at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:356:11)
    at ServerResponse.header (C:\Users\artys\Documents\GitHub\boutique-life\node_modules\express\lib\response.js:719:10)
    at ServerResponse.send (C:\Users\artys\Documents\GitHub\boutique-life\node_modules\express\lib\response.js:164:12)
    at done (C:\Users\artys\Documents\GitHub\boutique-life\node_modules\express\lib\response.js:956:10)
    at Object.exports.renderFile (C:\Users\artys\Documents\GitHub\boutique-life\node_modules\jade\lib\index.js:374:12)
    at View.exports.__express [as engine] (C:\Users\artys\Documents\GitHub\boutique-life\node_modules\jade\lib\index.js:417:11)
    at View.render (C:\Users\artys\Documents\GitHub\boutique-life\node_modules\express\lib\view.js:126:8)
    at tryRender (C:\Users\artys\Documents\GitHub\boutique-life\node_modules\express\lib\application.js:639:10)
    at EventEmitter.render (C:\Users\artys\Documents\GitHub\boutique-life\node_modules\express\lib\application.js:591:3)
    at ServerResponse.render (C:\Users\artys\Documents\GitHub\boutique-life\node_modules\express\lib\response.js:960:7)
"

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
leninlin, 2017-02-23
@leninlin

res.send can only be called once. When you try to send something else, just such an error occurs, which essentially says that the answer has already been sent.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question