I am trying to return a value from a Cypress task, by the running of a function:
///// <reference types="cypress" />
/**
* @type {Cypress.PluginConfig}
*/
var sqlite3 = require('sqlite3').verbose()
let db = new sqlite3.Database('/db/db.sqlite3', sqlite3.OPEN_READWRITE, (err) => {
if (err) {
console.error(err.message);
}
console.log('Connected to the database.');
})
function getInvoiceNumberRowId() {
db.each(`SELECT id FROM invoicerecord WHERE p_number = 7 and customer = 'John Doe'`, (err, row) => {
if (err) {
throw err;
}
console.log(`The invoice id is ${row.id}`)
const row_id = row.id
return row_id
})
}
module.exports = (on, config) => {
on('task', {
InvoiceNumberRowId() {
return getInvoiceNumberRowId()
}
// }
})
}
However, when I do cy.task('InvoiceNumberRowId')
I get the following error message:
The task 'InvoiceNumberRowId' returned undefined. You must return a value, null, or a promise that resolves to a value or null to indicate that the task was handled.
I don't understand why I am getting this error because the function returns row_id
.
The line console.log(`The invoice id is ${row.id}`)
works just as expected so I know that row.id
is defined.
I know this may be something to do with the Async nature of Javascript