如何在本地运行的 Azure 函数中获取连接字符串环境变量? [英] How to get a connection string environment variable in an Azure function running locally?

查看:13
本文介绍了如何在本地运行的 Azure 函数中获取连接字符串环境变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置一个 Azure 函数以在我的开发环境中本地运行.我希望连接到 MongoDb 数据库实例.

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

在我添加的 local.settings.json 文件中:

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

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

在我的函数中,我有:

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

process.env.DB_CONNECT_STRING 未定义.

我假设我需要为环境变量添加某种前缀,但我在任何地方都找不到此文档.如何指定连接字符串并在函数代码中引用?

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?

推荐答案

Matt Mason 是对的.

在 Node.js 中,我们应该在 Values 集合中指定应用设置.然后可以使用 process.env 将这些设置作为环境变量读取.

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"
  }
}

然后使用process.env.DB_CONNECT_STRING获取值.

这篇关于如何在本地运行的 Azure 函数中获取连接字符串环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆