3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ExpressのProgreSQL node-postgres Ver.6系⇒7系 記述方法比較

Last updated at Posted at 2019-02-16

#はじめに
node-postgresを使おうとして、
pg.connect is not a function がでたらVer違いによる記述方法の違いが原因なので
下記を参考に変更すると対応できると思います。
Ver.6系⇒Ver.7系で記述方法が大きく変わりました。

#pg@6までの記述方法

index.js
var express = require('express');
var router = express.Router();
var pg = require("pg");

router.get('/', function(req, res) {
  var conf = "tcp://user:password@localhost:5432/user_db";
  pg.connect(conf,function(err,client){
    if(err){
      console.log(err);
    } else{
      client.query("SELECT * from mydata",function(err,result){
        res.render("index",{
          datas : result.rows
        });
      });
    }
  });
});

module.exports = router;

#pg@7での記述方法

index.js
var express = require('express');
var router = express.Router();
var pg = require("pg");

router.get('/', function(req, res) {
  var pool = pg.Pool({
    database: 'user_db',
    user: 'user',
    password: 'password',
    host:"localhost",
    port: 5432
  });
  pool.connect(function(err,client){
    if(err){
      console.log(err);
    } else{
      client.query("SELECT * FROM mydata",function(err,result){
        res.render("index",{
          datas : result.rows
        });
      });
    }
  });
});
module.exports = router;

よかったら参考にしてください。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?