Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a connection string environment variable in an Azure function running locally?

I am trying to setup an Azure Function to run locally on my development environment. I wish to connect to a MongoDb database instance.

In my local.settings.json file I have added:

"ConnectionStrings": {
    "DB_CONNECT_STRING": "mongodb://localhost:27017/MyDatabase"
}

In my function I then have:

module.exports = function (context, myTimer) {
    console.log(process.env.DB_CONNECT_STRING);
    context.done();
};

process.env.DB_CONNECT_STRING is undefined.

I assume I need to add some kind of prefix to the environment variable, but I can't find this documented anywhere. How do I specify a connection string and reference it in the function code?

like image 291
infojolt Avatar asked Oct 21 '25 20:10

infojolt


1 Answers

Matt Mason is right.

In Node.js, we should specify app settings in the Values collection. These settings can then be read as environment variables by using process.env.

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "",
    "DB_CONNECT_STRING": "mongodb://localhost:27017/MyDatabase"
  }
}

enter image description here

Then use process.env.DB_CONNECT_STRING to get the value.

like image 139
Aaron Chen Avatar answered Oct 23 '25 11:10

Aaron Chen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!