简体   繁体   中英

Async await problem in nodejs sequelize connection string

 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))

I am using the above code to make connection to postgres database. But I am receiving below error on execution of node index command, index.js is not attached here. I want dbUSerName and dbUserpassword values to be passed in the sequelize connection string after fetched from the vault. but the values are promise which I am not able to resolve.

error: uncaughtException: The "string" argument must be of type string or an instance of Buffer or ArrayBuffer. Received an instance of Promise

credential function returns Promise, you need to call it as a promise function.

You can read about promises here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

I think it will be better to wrap your code in a async function and use await before calling credential


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

main();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM