gpt4 book ai didi

javascript - Node.js:WAITING需求

转载 作者:搜寻专家 更新时间:2023-11-01 00:48:45 26 4
gpt4 key购买 nike

我是新来的 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);

测试 1

./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 }

测试2

使 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();

结果

{}

测试 3

使 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com