2015-04-04 65 views
1

我尝试使用Bluemix平台中的Node.js获取postgresql的工作。但是,有一个问题。我试图使用https://www.ng.bluemix.net/docs/#starters/nodejs/index.html#PostgreSQL中描述的代码。无法将记录插入到Bluemix中的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平台得到任何错误。所以我认为可以插入访问记录。但是,似乎并非如此。

任何人都可以有任何指针来解决这个问题吗?

+0

也许尝试暂时取出“where”以确保其插入行。 – 2015-04-04 09:26:19

+0

检查您的日志,并添加更多的日志语句。有关登录Bluemix的更多信息,请参阅博客文章[“Node.js调试以更好的日志记录开始!”](https://ibm.biz/bluemix-logging) – 2015-04-04 12:25:32

+0

感谢这两者,在做了更多日志记录之后,我发现INSERT后,内部结果函数中的req.ip变得未定义。 – 2015-04-05 08:39:00

回答

3

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

所以我添加了一个本地变量来存储请求的IP以保留内容。

相关问题