- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我是新来的 Node.js
我一直在处理第三方提供商的示例项目,我正在尝试使用 Azure Key Vault 来存储配置值。
我在获取 wait
的过程时遇到问题在执行其余部分之前。我会尽量详细说明。
sample project有一个名为 agent.js
的文件这是起始页/文件。在 line 16 ( agent_config = require('./config/config.js')[process.env.LP_ACCOUNT][process.env.LP_USER]
) 它调用带有值的配置文件。我正在尝试使用 Key Vault 设置这些值。我尝试了很多调用函数的组合,甚至实现了 async / await
但是 agent_config
的值始终包含 [Promise]
对象,而不是 Key Vault 返回的数据。
如果我是对的,那是因为 Key Vault 本身使用 async / await
也会在返回 Key Vault 值之前返回配置文件。
在这种情况下如何添加/实现 Key Vault?
这是我尝试过的:
首先将agent.js更新为
let agent_config = {};
try {
agent_config = require('./config/config.js')['123']['accountName'];
} catch (ex) {
log.warn(`[agent.js] Error loading config: ${ex}`)
}
console.log(agent_config);
./config/config.js
const KeyVault = require('azure-keyvault');
const msRestAzure = require('ms-rest-azure');
const KEY_VAULT_URI = 'https://' + '{my vault}' + '.vault.azure.net/' || process.env['KEY_VAULT_URI'];
function getValue(secretName, secretVersion) {
msRestAzure.loginWithAppServiceMSI({ resource: 'https://vault.azure.net' }).then((credentials) => {
const client = new KeyVault.KeyVaultClient(credentials);
client.getSecret(KEY_VAULT_URI, secretName, secretVersion).then(
function (response) {
return response.Value;
});
});
}
module.exports = {
'123': {
'accountName': {
accountId: getValue('mySecretName', '')
}
}
};
结果
{ accountsId: undefined }
使 getValue 成为 async
函数并将其包装在另一个函数周围(尝试不包装但也没有工作)
./config/config.js
const KeyVault = require('azure-keyvault');
const msRestAzure = require('ms-rest-azure');
const KEY_VAULT_URI = 'https://' + '{my vault}' + '.vault.azure.net/' || process.env['KEY_VAULT_URI'];
async function getValue(secretName, secretVersion) {
msRestAzure.loginWithAppServiceMSI({ resource: 'https://vault.azure.net' }).then((credentials) => {
const client = new KeyVault.KeyVaultClient(credentials);
client.getSecret(KEY_VAULT_URI, secretName, secretVersion).then(
function (response) {
return response.Value;
});
});
}
async function config() {
module.exports = {
'123': {
'accountName': {
accountId: await getValue('mySecretName', '')
}
}
};
}
config();
结果
{}
使 getValue 成为 async
函数并将其包装在另一个函数周围(尝试不包装但也没有工作)
./config/config.js
const KeyVault = require('azure-keyvault');
const msRestAzure = require('ms-rest-azure');
const KEY_VAULT_URI = 'https://' + '{my vault}' + '.vault.azure.net/' || process.env['KEY_VAULT_URI'];
async function getValue(secretName, secretVersion) {
return msRestAzure.loginWithAppServiceMSI({ resource: 'https://vault.azure.net' })
.then((credentials) => {
const client = new KeyVault.KeyVaultClient(credentials);
return client.getSecret(KEY_VAULT_URI, secretName, secretVersion).then(
function (response) {
return response.Value;
});
});
}
module.exports = {
'123': {
'accountName': {
accountId: getValue('mySecretName', '')
}
}
};
config();
结果
{ accountId: { <pending> } }
我已经尝试过许多其他方式,例如 module.exports = async (value) =< {...}
(通过其他问题/解决方案发现没有成功。
我开始觉得我需要在 agent.js
上做一些“等待”但我还没有找到这方面的好信息。
任何帮助都会很棒!
最佳答案
一个问题是您的 getValue
函数没有返回任何内容,因为您的返回需要是明确的。
(如果没有返回 promise,就没有什么可等待的)
async function getValue(secretName, secretVersion) {
return msRestAzure.loginWithAppServiceMSI({ resource: 'https://vault.azure.net' })
.then((credentials) => {
const client = new KeyVault.KeyVaultClient(credentials);
return client.getSecret(KEY_VAULT_URI, secretName, secretVersion).then(
function (response) {
return response.Value;
});
});
}
您也可以使用箭头函数来避免显式返回。
const getValue = async (secretName, secretVersion) =>
msRestAzure.loginWithAppServiceMSI({ resource: 'https://vault.azure.net' })
.then(credentials => {
const client = new KeyVault.KeyVaultClient(credentials);
return client.getSecret(KEY_VAULT_URI, secretName, secretVersion)
.then(response => response.Value);
});
引入异步的 Azure Key Vault 读取意味着您的整个配置读取是异步的。你无能为力。这意味着使用配置的代码需要适本地处理它。您首先导出将返回配置的异步函数..
async function getConfig() {
return {
'123': {
'accountName': {
accountId: await getValue('mySecretName', '')
}
}
};
}
module.exports = getConfig;
在您的代理代码中调用该函数。这将意味着您的代理代码也需要包装在一个函数中,所以可能是这样的……
const Bot = require('./bot/bot.js');
const getConfig = require('./config/config.js');
getConfig().then(agentConfig => {
const agent = new Bot(agentConfig);
agent.on(Bot.const.CONNECTED, data => {
log.info(`[agent.js] CONNECTED ${JSON.stringify(data)}`);
});
});
关于javascript - Node.js:WAITING需求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54992520/
您能否提供有关网站社交网络(例如 Facebook)类型(功能性、非功能性和用户要求)要求的示例? 提前谢谢 最佳答案 以下是 Facebook 应具备的要求的一些示例。然而,值得一提的是:对于每种类
我需要在另一个 Java 项目的 liferay 模块项目中使用一些类。我正在使用 gradle,当我部署应用程序时,即使 gradle 编译了我的 jar 文件,我也会收到 Unresolved 需
我最近发现了类似于button.setText(“Hello World”);的代码行如果您按如下方式分配按钮,则 onCreate() 方法中将抛出 nullPointerException: B
我有一些基于成本的供应链管理代码,其中供应尝试满足需求,但这仅在供应大于需求时才有效。有什么方法可以优化它以两种方式工作(即当 supply > demand 和 supply = model.dem
好吧,我快想多了。有没有一种方法可以组合接口(interface)和属性,使实现类中的特性属性满足接口(interface)契约? 在我的应用程序中,我想显示一个事件列表,它是系统中事件的集合,例如新
我想创建一个模型,在每个步骤中预测每个产品在多周内的 future 需求(预测每个产品明年的每周需求) 我有一些小尺寸(大约 100-200 条记录)的 csv。 这里有关 CSV 列的信息:- 第一
我有一个包含我所有依赖项的 requirements.txt 文件,但它没有被正确处理: pip install -r requirements.txt 后,我得到以下 pip freeze: arg
我对 Java EE 应用程序的性能测量(CPU 和磁盘 I/O 需求)很感兴趣。 对于CPU 我已经想出了如何测量每个方法调用的CPU 需求。通过在每个方法的开始和结束时调用 java.lang.m
如何获取从 yaml 文件创建的管道的需求? yaml 文件包含需求: ... jobs: - job: my_job displayName: My Job pool: name:
我的目标: 构建一个 AngularJS 服务 (MapService),它初始化 (MapService.initMap()) 第 3 方控件 (Esri ArcGIS Map) 并返回对我的 ma
我在我的一个项目中使用了 redis,并且有一个带有 redis = Redis.new 的初始化器并使用了 redis gem。问题是,如果 Redis 没有运行,我将无法执行简单的数据库迁移之类的
如果我们有三个模块名称 A、B 和 C,那么模块 A 需要 B 和 B 需要 C:这个调用会产生什么效果? var A = proxyquire('A', {'C': mockedModule}) 模
我正在为 Liferay 7 开发一些功能。我知道仍处于 beta 版本,但我在 OSGi 包依赖项方面遇到了一些麻烦。当我尝试部署服务项目时,部署时发生错误 Unresolved requireme
我有一个 list 文件 partials.js,其中包含: //= require_tree ./partials 然后 ./partials/ 中的每个咖啡文件都包含以下内容: $ -> #
在 brew 中有没有一种方法或特殊命令可以将安装的包卡住到 requirements.txt 文件中,就像在 python 中使用 pip 一样?然后从该文件快速重新安装它们? 最佳答案 使用Hom
我正在尝试在 g1 GKE 实例(g1 实例有 1 个 vCPU,或 1000 毫核)中运行一个小型应用程序,并且在调度 pod 时遇到 CPU 请求限制问题。有 4 个 pod,每个都是应用程序的不
我们计划为我们的 C++ 代码建立柯南存储库。我们只想向开发人员公开依赖项列表 (lib/version@user/channel),而不是我们放在 conanfile.py 中的逻辑检查。这个包装器
我正在尝试研究 Varnish 。 我的问题是。我确实有一个主要请求/响应,我想使用 Varnish 缓存它。 html结构内部有ESI标签。我想要很多。一个标签具有较长的 ttl,其他标签则具有 t
我有一个在运行时或编译/链接时连接接口(interface)的问题或最佳方法。对于我的嵌入式项目,设备驱动程序和 I/O 具有需要绑定(bind)(即粘合)到其相应接口(interface)依赖项的接
我有一个我创建的协议(protocol)(在 Swift 4.2 中),它的要求之一是一个与协议(protocol)本身类型相同的属性。 例如,我有一个这样定义的协议(protocol): proto
我是一名优秀的程序员,十分优秀!