【问题标题】:How to connect Redshift database using Node JS如何使用 Node JS 连接 Redshift 数据库
【发布时间】:2021-09-02 01:47:27
【问题描述】:

我无法使用 Node.js 连接到 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');

【问题讨论】:

  • 我使用的是节点版本 12.13.1,问题已为我解决。
  • 我在 AWS Lambda 函数中使用的代码相同,但响应为空。为什么?
  • 请点击链接,此代码对我有用。 stackoverflow.com/questions/68080267/…

标签: node.js amazon-web-services aws-lambda amazon-redshift


【解决方案1】:

这个包似乎有点被遗弃了,因为这里有一个 open issue 有确切的问题

解决需要手动申请this solution

转到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",``` .是一样的吗?
【解决方案2】:

【讨论】:

    猜你喜欢
    • 2016-09-20
    • 2018-11-05
    • 1970-01-01
    • 2019-12-31
    • 2016-11-12
    • 2022-11-09
    • 2018-10-03
    • 1970-01-01
    • 2020-02-24
    相关资源
    最近更新 更多