简体   繁体   中英

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. In the function app, I want to query the incoming data against a azure sql database.

In the azure function->application settings->connection string, I created a connection string x with value of the azure db connection string. My index.js file is as below.

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. How can I access 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.

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

There is no easy way to access connection strings in 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 .

See similar questions one and two .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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