How do I create a function parameter (variable) for the answers to an inquirer.prompt question in javascript? MySQL is also involved

109
November 30, 2020, at 9:10 PM

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" );
Answer 1

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:

function addToTable ( prompt_name , table_name , table_col )

nested inside that function with the second part of my inquirer.prompt looking like this:

connection.query("INSERT " + table_name + " (" + table_col + ") VALUES ('" + data[prompt_name] + "')"

and finally calling the function like this:

addToTable("department_name" , "department" , "name" );

therefore i got data.department_name to go where I wanted it at the correct time!

READ ALSO
socket-io: How to chat separately between chatroom creator and other user and save them on database

socket-io: How to chat separately between chatroom creator and other user and save them on database

I made a chat app, where users can join in a chatroom and when a message is sent, I am saving those messages to db

49
activate next and privous button of owl-slider using jquery

activate next and privous button of owl-slider using jquery

hello i am new in laravel i am displaying product on silder it works fine but my next and privous button is not workingHow can i work on next and previous button its customize code

77
QueryDsl instead of MongoTemplate

QueryDsl instead of MongoTemplate

Could you please help me find out how to include the mongo collection name to a QueryDsl query?

63
Javascript sort an array of strings that containing numbers

Javascript sort an array of strings that containing numbers

I'm trying to sort an array of string that sometimes contain numbers

62