简体   繁体   English

如何使更新准备好的语句在 Node JS 的 JavaScript 中工作

[英]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. roleID、val.firstname 和 val.lastName 是很好的变量,因为它们在程序的其他地方进行了测试。 The update statement is not updating though.虽然更新语句没有更新。 I tried it with doublequotes around the where statement variables but that doesn't work.我尝试在 where 语句变量周围加上双引号,但这不起作用。 What am I doing wrong?我究竟做错了什么? The same statement works in a mysql shell.相同的语句适用于 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.我更改了代码以获取我想要更改的名称的 ID,而不是匹配 first_name 和 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.然后我使用 Name() 来获取询问者问题的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Node.JS for MSSQL中创建准备好的语句? - How do I create a prepared statement in Node.JS for MSSQL? 如何使用 JS 从键值数组为 sqlite UPDATE 构建准备好的语句? - How can I use JS to build a prepared statement for a sqlite UPDATE from an array of key values? 试图将变量传递到 JavaScript Node JS 中的准备好的语句中 - Trying to pass variables into a prepared statement in JavaScript Node JS 如何在Express.js中使用值数组和预准备语句更新MySQL中的表? - How do I update a table in MySQL using an array of values and a prepared statement in Express.js? 我怎么能在javascript中发表声明“for”等待 - how i can make statement “for” wait in javascript 如果在Javascript中运行语句,我似乎无法做到这一点: - I can't seem to make this if statement work in Javascript :S 准备好的语句未在MSSQL的节点JS中执行 - Prepared statement not executing in node js with mssql 如何让javascript代码与node.js一起工作? - How to make javascript code to work witn node.js? 如何将代码分成两个文件,并使其仍在node.js中工作? - How can I separate a code in two files, and still make it work in node.js? 关于node.js的Javascript:如何使代码将SQL值存储在数组中 - Javascript, regarding node.js: How can I make the code store, in an array, SQL values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM