How do I create a function parameter (variable) for the answers to an inquirer.prompt question in JavaScript? I know how to do this without using variables, but to make my function addToTable easier to use throughout my code, I wanted to use parameters. A concatenation is being used for a MySQL query in the answer portion of my inquirer.prompt question. My problem is how do I pass data.department_name as a variable inside the concatenation.
function addToTable ( prompt_name , prompt_answer , table_name , table_col ){
inquirer.prompt([
{
type: "input",
name: prompt_name,
message: "Type the " + prompt_name + " ?"
}
]).then(function (data) {
connection.query("INSERT " + table_name + " (" + table_col + ") VALUES ('" + prompt_answer + "')", function (err, res) {
if (err) throw err;
});
})
};
//then calling the function as so
addToTable("department_name" , "data.department_name" , "department", "name" );
I wish I searched for "Concatenate a dynamic variable name in Javascript" earlier. What worked for me was to put the parameter(prompt_name) in brackets. The function parameters looked as such:
nested inside that function with the second part of my inquirer.prompt looking like this:
and finally calling the function like this:
therefore i got data.department_name to go where I wanted it at the correct time!