I have a method for creating a database connection, and want to keep it generic to allow calling separate query methods from it. Tried doing:
async function connectAndQuery(functionCall, config) {
const mongoUri = `mongodb+srv://` + config.mongoUser + `:` + config[`${config.environment}MongoPassword`] + `@company-user-` + config.mongoEnvUriSection + `.mongodb.net/db-name-` + config.environment + `?retryWrites=true&w=majority`;
const client = new MongoClient(mongoUri);
try {
await client.connect();
return await functionCall(client);
Where functionCall
is the name of the method with the query, but functionCall
is not recognized as a function.
EDIT:
Added calling code which uses the task
function from Cypress IO.
From plugins/index.js
module.exports = (on, config) => {
on('task', {
queryDb({functionCall, config}) {
return connectAndQuery(functionCall, config)
}
})
config = dotenvPlugin(config)
return config
}
From support/index.js
cy.task('queryDb', {functionCall: 'getPasswordLink', config: Cypress.env()})