@websjunior

Почему не подключается к PostgreSQL на node js?

Есть в отдельной файле контроллеры, на пост должно выводить данные из бд. выводит ордер-1 и ордер-2 как нужно, а вот из бд ошибка в консоли.
/* 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();
		});
	});
});


А вот и главный файл в котором путь к бд
var pg = require('pg');

//DB connect
var connect = "postgres://andrey:123123@localhost:5432/bytik-life";

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

(Так же прикрепил подключение роутеров. В них пост и гет запросы.)
Так вот, по написанию синтаксических ошибок 0, а вот ошибку выписывает
"
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)
"
  • Вопрос задан
  • 731 просмотр
Пригласить эксперта
Ответы на вопрос 1
@leninlin
res.send можно вызывать только один раз. При попытке что-то еще отправить выпадает именно такая ошибка, которая по сути говорит, что ответ уже был отправлен.
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы