Azure 函数应用程序在 PostgreSQL pg 调用上失败

问题描述 投票:0回答:1

我正在尝试模拟从 Azure IoT 中心写入 PostgreSQL 的示例,如本示例中所述。

我已成功将所有内容正确设置到触发功能的位置。但是,文章中提供的代码:

module.exports = async function (context, IoTHubMessages) { var pg = require('pg'); //const config = "postgres://<username>:<password>@<postgres servername>:5432/<database>"; const config = { host: 'myserver.postgres.database.azure.com', user: 'granted@myserver', password: 'password', database: 'telemetry', port: 5432, ssl: true }; var client = new pg.Client(config); const query = 'insert into tempdata(deviceid, data) values(' + eventHubMessages.deviceId + ',\'' + JSON.stringify(eventHubMessages) + '\');'; context.log(query); client.connect(); client.query(query); await client.end(); context.log('insert completed successfully!'); /* client.connect(err => { if (err) throw err; else { client .query(query) .then(() => { context.log('insert completed successfully!'); client.end(console.log('Closed client connection')); }) .catch(err => console.log(err)) .then(() => { context.log('Finished execution, exiting now'); }); } }); */ };
产生此错误:

结果:失败异常:找不到模块“pg”需要堆栈:- /home/site/wwwroot/IoTHub_EventHub2/index.js

我想我必须在函数中安装 PostgreSQL 或者其他东西?任何帮助将不胜感激。

postgresql azure azure-functions azure-iot-hub
1个回答
0
投票
问题在于尝试从门户安装并运行该功能。无法通过该机制添加依赖项,例如 PostgreSQL。在 Visual Studio Code 中设置一个项目,我能够在本地运行 npm,然后将依赖项添加到项目中,当我安装时,它解决了这个问题。

© www.soinside.com 2019 - 2024. All rights reserved.