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

查看:114
本文介绍了如何在本地运行的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文件中添加了:

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

推荐答案

马特·梅森是正确的.

在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天全站免登陆