简体   繁体   中英

How can I make a update prepared statement work in JavaScript in Node JS

.then(function (val) {
      var roleId = roleArr.indexOf(val.role) + 1;
      db.query(
        'UPDATE employee SET role_id = ? WHERE first_name = ? AND last_name = ?;',
        [roleId, val.firstname, val.lastName],
        function (err) {
          if (err) throw err;
          console.table(val);
          startPrompt();
        }
      );
    });

This code comes from an inquirer statement. The roleID, val.firstname, and val.lastName are good variables because they are tested elsewhere in the program. The update statement is not updating though. I tried it with doublequotes around the where statement variables but that doesn't work. What am I doing wrong? The same statement works in a mysql shell.

I changed the code to get the id of the name I wanted to change instead of matching a first_name and last_name.

var nameArr = [];
function Name() {
  db.query(
    "SELECT * FROM employee",
    function (err, res) {
      if (err) throw err;
      for (var i = 0; i < res.length; i++) {
        nameArr.push(res[i].first_name + " " + res[i].last_name);
      }
    });
  return nameArr;
}

and

var nameId = nameArr.indexOf(val.name) + 1;

Then I used the Name() to get the choices for the inquirer question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM