【问题标题】:How to use async knexfile如何使用异步 knexfile
【发布时间】:2020-12-15 19:25:12
【问题描述】:

我在尝试将配置导出为异步函数时遇到了 knex 问题。

我的 knex 文件是:

async function fetchConfiguration() {
  
  return {
    staging:{
      client: 'mysql',
      connection: {
      host : 'dbIp',
      user : 'user',
      password : 'password',
      database : 'myDatabase'
      }
    }
  }
  
}

module.exports = async () => {
  const configuration = await fetchConfiguration();
  return {
    ...configuration
  }
};

我的 connection.js 是:

const knex = require('knex');
const configuration = require('../../knexfile');

var connection = knex(configuration.staging);

module.exports = connection;

但是在运行时,我得到了这个错误

C:\Users\fabio\Documents\Projetos_CVC\uptime\uptime\API\node_modules\knex\lib\knex.js:22
  if (arguments.length === 0 || (!config.client && !config.dialect)) {
                                         ^

TypeError: Cannot read property 'client' of undefined
    at Knex (C:\Users\fabio\Documents\Projetos_CVC\uptime\uptime\API\node_modules\knex\lib\knex.js:22:42)
    at Object.<anonymous> (C:\Users\fabio\Documents\Projetos_CVC\uptime\uptime\API\src\database\connection.js:4:18)
    at Module._compile (internal/modules/cjs/loader.js:1137:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
    at Module.load (internal/modules/cjs/loader.js:985:32)
    at Function.Module._load (internal/modules/cjs/loader.js:878:14)
    at Module.require (internal/modules/cjs/loader.js:1025:19)
    at require (internal/modules/cjs/helpers.js:72:18)
    at Object.<anonymous> (C:\Users\fabio\Documents\Projetos_CVC\uptime\uptime\API\src\controllers\CheckController.js:1:20)
    at Module._compile (internal/modules/cjs/loader.js:1137:30)

很快我就需要在这个项目中使用 vault 和 consul,所以不使用异步创建很复杂。 感谢您对此提供的任何帮助。

【问题讨论】:

    标签: javascript node.js knex.js async.js hashicorp-vault


    【解决方案1】:

    由于您的配置是异步的,因此您需要等待它。

    这意味着为了配置连接,您的代码应该一直是异步的。

    它应该看起来像:

    // dbConfig.js
    async function fetchConfiguration() {
      
      return {
        staging:{
          client: 'mysql',
          connection: {
          host : 'dbIp',
          user : 'user',
          password : 'password',
          database : 'myDatabase'
          }
        }
      }
      
    }
    
    module.exports = async () => {
      const configuration = await fetchConfiguration();
      return {
        ...configuration
      }
    };
    
    // db.js
    const knex = require('knex');
    const configuration = require('../../dbConfig');
    
    const config = await configuration();
    // --------------^ you are exporting an async function, it must be awaited
    
    const connection = knex(config.staging);
    module.exports = connection;
    

    ps,top level await 并非所有节点版本都支持,请检查您的是否支持。如果没有,您可以按照链接中的说明使用 iffe 包装代码。

    【讨论】:

      猜你喜欢
      • 2022-11-05
      • 2020-06-14
      • 2012-01-20
      • 2017-09-03
      • 2020-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多