【问题标题】:Async await problem in nodejs sequelize connection stringnodejs sequelize连接字符串中的异步等待问题
【发布时间】:2022-01-14 07:39:03
【问题描述】:

function credential(secretFromVault) {
  const creddetails = new ClientSecretCredential(clientId, tenantId, cleintSecret);
  // Build the URL to reach your key vault
  const url = `https://<vaultName>.vault.azure.net/`;

  // Lastly, create our secrets client and connect to the service
  const client = new SecretClient(url, creddetails);

  const secretName = secretFromVault;
  return new Promise(resolve => {
    client.getSecret(secretName).then(latestSecret => {
      console.log(`value from secret is`, latestSecret.value);
      resolve(latestSecret.value)
    })
  })
}
const dbUserName = credential(constants.pgDbUserName)
const dbPassword = credential(constants.pgDbPassword)

const hostname = constants.pgHostname;
const port = constants.pgPort;
const dbName = constants.pgDbName;
const sequelize = new Sequelize(dbName, dbUserName, dbPassword, {
  host: hostname,
  port: port,
  dialect: constants.dialectName,
  logging: false,
  pool: {
    max: constants.pgMaxPoolConnections,
    min: constants.pgMinPoolConnections,
    acquire: constants.pgMakeConnectionTimeOut,
    idle: constants.pgIdleTimeout
  }
});
sequelize.authenticate()
  .then(() => {
    console.log('Successfully connected.');
    User.sync();
    Credentials.sync();
    App.sync();
    Entity.sync();
    EntityField.sync();
    Relationship.sync();
  })
  .catch(err => console.log('Error: ' + err))

我正在使用上面的代码连接到 postgres 数据库。但是我在执行节点索引命令时收到以下错误,这里没有附加 index.js。 我希望 dbUSerName 和 dbUserpassword 值在从 Vault 中获取后传递到 sequelize 连接字符串中。但这些价值观是我无法解决的承诺。

错误:uncaughtException:“字符串”参数必须是字符串类型或 Buffer 或 ArrayBuffer 的实例。收到一个 Promise 实例

【问题讨论】:

    标签: javascript node.js async-await sequelize.js


    【解决方案1】:

    credential函数返回Promise,需要将其作为promise函数调用。

    您可以在此处阅读有关承诺的信息 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

    我认为将代码包装在异步函数中并在调用credential之前使用await会更好

    
    async function main() {
        const dbUserName = await credential(constants.pgDbUserName);
        const dbPassword = await credential(constants.pgDbPassword);
     
        // Your code
    }
    
    main();
    

    【讨论】:

    • 在 sequelize.authenticate 之后,我使用 module.exports 导出 sequelize 并将其用于 sequelize.define()。如果我在 main() 函数中添加它,那么 dbUserName 和 dbPassword 没有为 const sequelize = new Sequelize(dbName, dbUserName, dbPassword所以,这个 module.exports = { sequelize } 不起作用。我不明白,我应该导出函数吗?
    • 可以,可以导出异步函数
    • 如果我导出函数,然后使用这个 const dbConnection = require('../config/dbconfig')//上面的配置文件 const Credentials = dbConnection.sequelize.define('credentials' ,{,我收到以下错误 TypeError: Cannot read property 'define' of undefined
    • 你是从主线返回续集吗?
    • 是的,我将它保存在 main 中,如果我将我的 sequelize 语句保存在 main 之外,那么它会在从 vault 收到凭据之前执行,并且我得到了预期的字符串,但收到了承诺
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多