pg.connect不是功能吗? [英] pg.connect not a function?

查看:82
本文介绍了pg.connect不是功能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎有很多文档(例如 https: //devcenter.heroku.com/articles/heroku-postgresql#connecting-in-node-js ,以及包括此站点在内的其他地方),表明与pg.js Node包进行连接的正确方法是使用pg .连接.但是,在先前的实际代码问题之后,我尝试使用上述Heroku文档中显示的确切代码进行测试:

There appears to be a lot of documentation (e.g. https://devcenter.heroku.com/articles/heroku-postgresql#connecting-in-node-js, but also elsewhere including this site) indicating that the proper method of connecting with the pg.js Node package is using pg.connect. However, I attempted (after previous problems with my actual code) to test by using the exact code shown on the aforementioned Heroku documentation:

var pg = require('pg');

pg.defaults.ssl = true;
pg.connect(process.env.DATABASE_URL, function(err, client) {
  if (err) throw err;
  console.log('Connected to postgres! Getting schemas...');

  client
    .query('SELECT table_schema,table_name FROM information_schema.tables;')
    .on('row', function(row) {
      console.log(JSON.stringify(row));
    });
});

我得到了错误消息"pg.connect不是一个函数".发生了什么,我该如何解决?

And I got the error message "pg.connect is not a function". What is going on, and how do I fix it?

推荐答案

pg的新版本(即7.0.0)大约在15个小时前(从我撰写本文时开始)发布.

A new version of pg, namely 7.0.0, was published about 15 hours ago (from the time I'm writing this).

此版本有很多更改,其中之一是pg.connect已被硬性弃用(换句话说:已删除),而取而代之的是pg.Pool(...).connect(...),如此处所述:

This version has lots of changes, one of them being that pg.connect has been hard-deprecated (in other words: removed) in favor of pg.Pool(...).connect(...), as documented here: https://node-postgres.com/guides/upgrading

新的连接方法如下:

var pool = new pg.Pool()

// connection using created pool
pool.connect(function(err, client, done) {
  client.query(/* etc, etc */)
  done()
})

// pool shutdown
pool.end()

许多较旧的文档不会反映这些更改,因此它们使用的示例代码将不再起作用.

Lots of older documentation will not reflect these changes, so the example code they use won't work anymore.

您可以尝试重写示例代码以使其在7.0.0中运行,或者显式安装仍可与示例代码一起使用的旧版本:

You can either try and rewrite the example code so it works in 7.0.0, or explicitly install an older version that will still work with the example code:

npm install pg@6

这篇关于pg.connect不是功能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆