I have an application that queries a mysql database. It starts out:
function init() {
console.log(`
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
What would you like to do?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
`);
return inquirer
.prompt({
type: "list",
name: "options",
message: "Select one",
choices: list,
})
.then((data) => {
return redirectQuestion(data.options);
})
.then(() => init())
.catch(err => {
console.log(err);
})
}
init()
The redirectQuestion function takes the user to a function with switch cases to find the correct query, for example:
class View {
constructor(table = '') {
this.table = table
this.statement = `SELECT * FROM ${this.table} ORDER BY id`
}
getTable() {
db.query(
this.statement, (err, results, fields) => {
if (err) {
console.log("There was an error with your request")
}
console.table(results)
}
)
}
}
The problem is, that after this runs, there is a recursive call to init() to bring up the options again. The options, instead of appearing below the data from the database, appear above them, and if I move to select other options, it will then create another table which overwrites the bottom part of the data.
Now, it’s probably a less than desirable way to write the function out, but I need to have a way to recall the table, and still display the data, and I am not sure what is going wrong.