【问题标题】:Azure functions integration with Postgres in Node.jsAzure 函数与 Node.js 中的 Postgres 集成
【发布时间】:2020-03-04 23:10:35
【问题描述】:

我想从 Azure 函数访问 Azure Postgres DB。 下面是Azure函数的node.js源码。

 module.exports = async function (context, req) {
try {
    context.log('Start function!');
    var pg = require('pg');

    const config = {
        host: 'taxiexample.postgres.database.azure.com',
        user: 'postgres@taxiexample',
        password: 'QscBjk10;',
        database: 'taxi',
        port: 5432,
        ssl: false
    };

    var client = new pg.Client(config);
    const query = 'insert into taxi values (\'2\');';
    context.log(query);
    client.connect();
    var res = client.query(query);
    await client.end();

    context.log(res);

} catch (e) {
    context.log(e);
} finally {
    context.res = {
        status: 200,
        body: "ok"
    };
}

};

记录没有插入,res对象返回如下错误:

2020-03-04T23:03:59.804 [Information] Promise {
  <rejected> Error: Connection terminated
      at Connection.<anonymous> (D:\home\site\wwwroot\HttpTrigger1\node_modules\pg\lib\client.js:254:9)
      at Object.onceWrapper (events.js:312:28)
      at Connection.emit (events.js:228:7)
      at Socket.<anonymous> (D:\home\site\wwwroot\HttpTrigger1\node_modules\pg\lib\connection.js:78:10)
      at Socket.emit (events.js:223:5)
      at TCP.<anonymous> (net.js:664:12)
}
2020-03-04T23:03:59.811 [Information] Executed 'Functions.HttpTrigger1' (Succeeded, Id=944dfb12-095d-4a28-a41d-555474b2b0ee)

你能帮帮我吗?谢谢

【问题讨论】:

  • 能否尝试使用.catch()的方法获取详细的错误信息?
  • 我已经插入了 .catch() 子句,但是代码没有启动任何异常。
  • 您能检查一下您数据库的防火墙吗?您是否在防火墙中打开了Allow access to Azure services。另外,请问您是否启用了Enforce SSL connection(docs.microsoft.com/en-us/azure/postgresql/…)?如果您这样做了,请在您的代码中将ssl 设置为true 或禁用它。

标签: node.js postgresql azure azure-functions azure-postgresql


【解决方案1】:

我已经解决了,这是一个微不足道的编程错误。 下面是正确的源码

module.exports = async function (context, req) {

    try {
        context.log('Start function!');
        var pg = require('pg');

        const config = {
            host: 'example.postgres.database.azure.com',
            user: 'postgres@example',
            password: 'passwd;',
            database: 'test',
            port: 5432,
            ssl: true
        };

        var client = new pg.Client(config);
        const query = 'insert into test values (\'2\');';
        context.log(query);

        client.connect(err => {
            if (err) {
                console.error('connection error', err.stack);
            } else {
                console.log('connected');
                client.query(query, (err, res) => {
                    if (err) throw err;
                    console.log(res);
                    client.end();
                })
            }
        });

    } catch (e) {
        context.log(e);
    } finally {
        context.res = {
            status: 200,
            body: "ok"
        };
    }

};

【讨论】:

  • 请解释一下 - 你做了什么改变?我面临着类似的问题......你只打开 SSL 吗?我看到你还使用了连接函数的回调 - 还有什么?
猜你喜欢
  • 2021-08-08
  • 1970-01-01
  • 1970-01-01
  • 2023-03-03
  • 2016-11-02
  • 2021-08-31
  • 2011-06-21
  • 2020-12-18
  • 2021-05-07
相关资源
最近更新 更多