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