TypeError:当用Heroku node.js调用pg.connect时,无法调用null的方法'query' [英] TypeError: Cannot call method 'query' of null - when calling pg.connect with Heroku node.js

查看:171
本文介绍了TypeError:当用Heroku node.js调用pg.connect时,无法调用null的方法'query'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了通过Node.js连接到Heroku postgres数据库的问题。我发现有人经历过这个问题,但他们的建议在我的情况下不起作用。



我将var DB_URL定义为存储的完整Postgres数据库URL由Heroku。我这样做是因为process.env.DATABASE_URL未定义。 (这是其他堆栈溢出帖子的建议)。



尝试连接的代码是:

 pg.connect(DB_URL,function(err,client){
client.query(...)


运行领班时:

  client.query('INSERT INTO bookmarks用户名,标题,图片,网址)VALUES(
^
TypeError:无法调用null的查询方法'

其中null表示它指的是要传递给pg.connect匿名函数的客户端对象。



建议感激,我在Heroku文档和谷歌搜索引擎上看到了很多和无用的东西。

我有同样的东西问题与我的网站和更令人沮丧的是,它在部署的网站上工作,但当我试图本地运行该网站(使用工头)时,我得到了这个错误
从错误我明白我失败连接到数据库并进一步检查这些是导致此错误的两个原因:
$ b $ ol
  • 没有DATABASE_URL环境变量
    出于某种原因,我没有在我的.env文件中设置 process.env.DATABASE_URL ,尽管它在远程站点上设置正确。要解决此问题,您可以运行heroku config并将所需的DATABASE_URL复制到本地.env文件(如果此文件不存在,请创建一个)。 重要提示:当您复制粘贴时,变量会将:字符改为=。

  • b>?ssl = true不是DATABASE_URL的一部分
    由于上述修复,客户端现在知道连接的位置,但由于身份验证问题仍然无法打开连接。通过将?ssl = true 添加到DATABASE_URL,它终于为我解决了这个问题。看起来,当您尝试从远程站点(heroku服务器)连接时,您不需要传递参数,但是当您尝试从本地计算机连接到数据库时,您需要使用此身份验证来进行正确连接。


    希望它能帮上忙。


    I am encountering an issue connecting to a Heroku postgres database via Node.js. I have found one other instance of somebody experiencing this issue but their suggestion does not work in my case.

    I define var DB_URL to be the full Postgres database URL that is stored by Heroku. I do this because process.env.DATABASE_URL is not defined. (This was the suggestion of the other stack overflow post).

    The code attempting the connection is:

    pg.connect(DB_URL, function(err, client) {
        client.query( ... )
    

    When running foreman:

    client.query('INSERT INTO bookmarks (username, title, image, url) VALUES (       
    ^
    TypeError: Cannot call method 'query' of null
    

    Where by null it is referring to the client object that is meant to be passed into the pg.connect anonymous function.

    Advice appreciated, I've looked high and low around the Heroku docs and Googled-a-plenty to no avail.

    解决方案

    I had the same problem with my site and what was more frustrating is that it worked on the deployed site but when I was trying to run the site locally (using foreman) I got this error. From the error I understood that I fail to connect to the database and from further checking these are the two reasons that caused this error:

    1. No DATABASE_URL environment variable
      For some reason I did not have the process.env.DATABASE_URL set in my .env file although it was set properly on the remote site. To solve this issue you can run "heroku config" and copy the required DATABASE_URL to your local .env file (if this file does not exist, create one).
      Important NOTE: When you copy paste the variables change the ":" char to "=".

    2. ?ssl=true is not part of the DATABASE_URL
      Thanks to the above fix the client now knows where to connect but it still could not open connection due to authentication issues. By adding ?ssl=true to the DATABASE_URL it finally solved the issue for me. It seems that when you are trying to connect from the remote site (heroku servers) you do not need to pass the parameter but when you are trying to connect to the database from your local computer you need this authentication to make proper connection.

    Hope it will help.

    这篇关于TypeError:当用Heroku node.js调用pg.connect时,无法调用null的方法'query'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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