简体   繁体   English

如何从js中的Azure函数查询数据库

[英]How to query a database from an Azure function in js

I have an IOT hub getting some data. 我有一个物联网中心,正在获取一些数据。 I also have an Azure function app in js that is triggered when an IOT event occurs. 我也有一个在IOT事件发生时触发的js中的Azure函数应用程序。 In the function app, I want to query the incoming data against a azure sql database. 在功能应用程序中,我想针对蔚蓝的sql数据库查询传入的数据。

In the azure function->application settings->connection string, I created a connection string x with value of the azure db connection string. 在azure函数->应用程序设置->连接字符串中,我创建了一个连接字符串x,其值为azure db连接字符串。 My index.js file is as below. 我的index.js文件如下。

module.exports = function (context, IoTHubMessages) {
context.log(`JavaScript eventhub trigger function called for message array ${IoTHubMessages}`);

IoTHubMessages.forEach(message => {
    context.log(`Processed message ${message}`);

 var sqlConnection = x;

});

context.done();

}; };

I get an error that x is not defined. 我收到一个未定义x的错误。 How can I access x? 如何访问x? Also how how to execute a select query from here. 以及如何从此处执行选择查询。

Any help will be greatly appreciated. 任何帮助将不胜感激。

Try accessing your app setting using process.env["x"] Here are the docs , and here's a related issue you may face if you're trying to run this locally. 尝试使用process.env["x"]访问您的应用设置,这是docs ,如果您试图在本地运行此文档 ,可能会遇到一个相关问题

I don't have experience writing Javascript functions to execute a select query, but this documentation seems like a good place to start: Use Node.js to query Azure SQL Database 我没有编写Javascript函数以执行选择查询的经验,但是该文档似乎是一个不错的起点: 使用Node.js查询Azure SQL数据库

There is no easy way to access connection strings in Node.js. 在Node.js中没有简单的方法来访问连接字符串。

Instead, the suggestion is to use app settings for all your secrets and connection strings. 相反,建议您对所有机密和连接字符串使用应用程序设置。 You can them access them using process.env.YourAppSetting . 您可以使用process.env.YourAppSetting访问它们。

See similar questions one and two . 看到类似的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM