如何使用Node.js连接Redshift数据库

7

我无法使用Node连接到Redshift数据库。我正在使用Node JS版本14.15.1。 是否与该版本有关的问题?

以下代码,我在本地机器上尝试过, redshift.js文件

var Redshift = require('node-redshift');
 
var client = {
    user: 'user',
    database: 'db',
    password: 'password',
    port: port,
    host: 'hostname',
};

var redshiftClient = new Redshift(client, {rawConnection:true});
 
module.exports = redshiftClient;

以下是从数据库获取值的代码,位于index.js文件中。
var redshiftClient = require('./redshift.js');

console.log('Before Connection');
redshiftClient.connect(function(err){
console.log('After Connection');
    if(err) throw err;
    else{
      redshiftClient.query('SELECT * FROM "customer"', {raw: true}, function(err, data){
        if(err) throw err;
        else{
          console.log(data);
          redshiftClient.close();
        }
      });
    }
  });

如果我运行这段代码没有出现错误,甚至这一行也没有执行:console.log('After Connection');

我使用了Node版本12.13.1,问题已经解决。 - Sriguru Velusamy
我在AWS Lambda函数中使用了相同的代码,但是为什么会得到空响应呢? - Sriguru Velusamy
请点击以下链接,这段代码在我的电脑上可以运行。https://stackoverflow.com/questions/68080267/cant-return-any-value-while-connecting-redshift-from-node-js - Sriguru Velusamy
使用连接池代替原始连接https://github.com/dmanjunath/node-redshift#connection-pooling。 - krishnakumar Balasubramaniam
2个回答

3

这个包似乎有点被遗弃了,因为这里有一个未解决的问题与你所遇到的问题非常相似。

要解决这个问题,你需要手动应用这个解决方案

前往node_modules/node-redshift并替换文件。

  "dependencies": {
    "commander": "^2.9.0",
    "migrate": "^0.2.2",
    "pg": "^6.1.2",
    "sql-bricks": "^1.2.3"
  },

package.json

  "dependencies": {
    "commander": "^2.9.0",
    "migrate": "^0.2.2",
    "pg": "^8.1.5",
    "sql-bricks": "^1.2.3"
  },

在此目录中运行npm install以更新此包。之后您的代码将可以正常工作。


node_modules/node-redshift 文件夹中,你提到了 "pg": "^6.1.2", 但是在 package.json 文件中,pg 的版本不同,为 "pg": "^8.1.5",。 这是相同的吗? - Sriguru Velusamy

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接