1

我正在尝试将环境变量切换到 Google Secrets Manager,但遇到了问题。我正在运行一个 expressjs api 试图建立一个数据库连接。但无论我尝试什么,它只会返回Promise { <pending> }而不是等待异步函数完成。非常感谢任何帮助!

gcloud.js

const { SecretManagerServiceClient } = require('@google-cloud/secret-manager');
const client = new SecretManagerServiceClient();

async function getSecret(name) {

const name = `projects/PROJECT-ID/secrets/${name}/versions/latest`;

const [version] = await client.accessSecretVersion({
  name: name,
});

const payload = version.payload.data.toString();

return payload;
}

module.exports.getSecret = getSecret;

配置.js

const gcloud = require('./config/gcloud.js')

const config = {
  authSecret: gcloud.getSecret(SECRET_NAME)
}

module.exports = config
4

1 回答 1

0

您需要await以下结果gcloud.getSecret

// Not actually valid
const config = {
  authSecret: await gcloud.getSecret(SECRET_NAME)
}

但是,顶级等待尚未得到广泛支持,因此您需要在函数中构建配置:NodeJS Async / Await - 使用 API 调用构建配置文件

于 2021-03-19T17:30:29.797 回答