node.js - 无法在 Bluemix 中将记录插入到 PostgreSQL

标签 node.js postgresql ibm-cloud

我尝试让 postgresql 在 Bluemix 平台上与 Node.js 一起工作。但是,有一个问题。我尝试使用 https://www.ng.bluemix.net/docs/#starters/nodejs/index.html#PostgreSQL 中描述的代码.

我可以在本地环境中运行我成功编写的应用程序。但是,它在 Bluemix 平台中无法正常运行。

以下是描述我所做的代码:

var recordVisit = function(req, res) {
    /* Connect to the DB and auth */
    pg.connect(psql, 
        function(err, client, done) {
            if (err) {
                return console.error('Error requesting client', err);
            }

            console.log(req.ip);

            client.query('insert into ips(ip, ts) values($1, $2)', [req.ip, new Date()],
                function(err, result) {
                    if (err) {
                        done();
                        return console.error('Error inserting ip', err);
                    }

                    client.query('select count(ip) as count from ips where ip=$1',
                        [req.ip], 
                        function(err, result) {
                            done();
                            if (err) {
                                return console.error('Error querying count', err);
                            }
                            console.log("You have visited " + result.rows[0].count + " times")
                            // res.writeHead(200, {'Content-Type': 'text/html'});
                            // res.end();
                        }
                    );
                }
            );
        }
    );
};

它遵循 bluemix 文档中的代码。 Bluemix 中访问计数的结果始终为零 (0)。但是在本地环境下的结果还可以。而且我没有从 Bluemix 平台收到任何错误。所以我假设可以插入访问记录。然而,似乎不是。

谁能指点我解决这个问题?

最佳答案

我试图在插入函数中插入更多日志记录。我发现在 Bluemix 平台中 INSERT SQL 的结果中变量 req.ip 变得未定义。

所以为了保持内容,我加了一个局部变量来存放请求的ip。

关于node.js - 无法在 Bluemix 中将记录插入到 PostgreSQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29444719/

相关文章:

node.js - glob匹配,排除所有JS文件

node.js - NodeJs : Exiftool can not be spawned in AWS Lambda environment

node.js - 无法与简单的 Docker Node.js 网络应用程序通信

java - postgresql 通知 channel

php - 在 Bluemix 中激活 PHP 扩展

node.js - CoffeeScript 类中的方法别名 - TypeError : Property is not a function

postgresql - 用于向 Postgres DB 批量插入的 Azure 持久功能

postgresql - Docker: Springboot container cannot connect to PostgreSql Container 连接报错

docker - 是否可以在 IBM Containers for Bluemix 中运行 ASP.NET Core 应用程序?

encryption - MessageHub - 静态数据加密