Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
972 views
in Technique[技术] by (71.8m points)

javascript - Knex: Cannot read property 'client' of undefined with async connection

For some reason I get TypeError: Cannot read property 'client' of undefined which refers to the second line of code in knex.js (see code below) when accessing the database with const knex = require('./knex');. It works when I don't have the async around the export but then I can't fetch the credentials for production and staging.

I have a knexfile.js file with the following setup:

const { getAwsDbCredentials } = require('./config/aws');
    
const defaultConfig = {
  client: 'mysql',
  pool: {
    afterCreate(connection, callback) {
      connection.query("SET time_zone='+00:00';", (err) => {
        callback(err, connection);
      });
    },
  },
  migrations: {
    directory: './migrations',
  },
};

const defaultConnectionConfig = {
  charset: 'utf8mb4',
  dateStrings: true,
  timezone: 'UTC',
  typeCast(field, next) {
    if (field.type === 'TINY' && field.length === 1) {
      const value = field.string();
      return value ? value === '1' : null;
    }
    return next();
  },
};

const rdsConnectionConfig = async () => {
  const { SecretString } = await getAwsDbCredentials();

  const { dbname, host, password, port, username } = JSON.parse(SecretString);

  return {
    ...defaultConnectionConfig,
    database: dbname,
    host,
    password,
    port,
    user: username,
  };
};

module.exports = async () => {
  return {
    test: {
      ...defaultConfig,
      connection: {
        ...defaultConnectionConfig,
        user: 'xxx',
        password: 'xxx',
        database: 'xxx',
      },
      seeds: {
        directory: './seeds/test',
      },
    },
    development: {
      ...defaultConfig,
      connection: {
        ...defaultConnectionConfig,
        user: 'xxx',
        password: 'xxx',
        database: 'xxx',
      },
      seeds: {
        directory: './seeds/development',
      },
    },
    production: {
      ...defaultConfig,
      connection: await rdsConnectionConfig(),
      seeds: {
        directory: './seeds/production',
      },
    },
    staging: {
      ...defaultConfig,
      connection: await rdsConnectionConfig(),
      seeds: {
        directory: './seeds/staging',
      },
    },
  };
};

And a knex.js file with the following setup:

const config = require('../../../knexfile.js')[process.env.NODE_ENV];

module.exports = require('knex')(config);
question from:https://stackoverflow.com/questions/66060492/knex-cannot-read-property-client-of-undefined-with-async-connection

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You are exporting a function, which returns an object, therefore, you need to invoke it.

const getConfig= require('../../../knexfile.js')

module.exports = require('knex')(getConfig()[process.env.NODE_ENV]);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...