title: Express js with mysql update only fields that are passed
body: <p>I've been trying to setup a rest API for a few days now.  I've been following a great tutorial that really helped me understand a large part of how these work (sending requests, getting responses, etc).  However it uses MongoDB and Mongoose.  I'm using MySQL.  My tables and views are a bit complicated so I decided instead of using an ORM I'd just use mysql2 package and do the querying myself.  I'm stuck with trying to <code>PATCH</code> and <code>PUT</code> at the moment.  Part of my front end functions by sometimes only sending 1 or 2 fields that need to be updatd (a <code>PATCH</code> from everything I've gathered).  So I used part of the MongoDB and Mongoose tutorial to build an array of objects and pass them into <code>connection.query</code>. Here's my patch route:</p>
  
  <pre><code>router.patch('/:txnid', (req, res, next) =&gt; { //UPDATE fields that are passed
      const txnid = req.params.txnid;
  
       for (const field of req.body) {
           fieldsToUpdate[field.name] = field.value;
       }
  
      connection.query("UPDATE QuoteToClose SET ? WHERE qb_TxnID = '" + txnid + "'", { fieldsToUpdate }, function (error, results) {
          if (error) {
              res.status(404).json({
                  message: error,
                  field: fieldsToUpdate
              });
          } else {
              res.status(201).json({
                  "record_count" : results.length,
                  "error": null,
                  "response": results
              });
          }
      });
  });
  </code></pre>
  
  <p>Sometimes I will pass 1 field, sometimes I will pass 2.  In this case I'm only passing 1. I build my body in POSTMAN and send this with the PATCH request:</p>
  
  <pre><code>[
      {
      "name": "margin",
      "value": "50"
      }
  ]
  </code></pre>
  
  <p>When I run this through POSTMAN I get the error:  </p>
  
  <pre><code>{
      "message": {
          "code": "ER_BAD_FIELD_ERROR",
          "errno": 1054,
          "sqlState": "42S22",
          "sqlMessage": "Unknown column 'fieldsToUpdate' in 'field list'"
      },
      "field": {
          "margin": "50"
      }
  }
  </code></pre>
  
  <p>I'm not sure why though.  I'm not using Mongoose unfortunately so I don't know if something is dependent on it that I'm missing.  My body parser is set like this:</p>
  
  <pre><code>app.use(bodyParser.urlencoded({extended: false}));
  app.use(bodyParser.json());
  </code></pre>
  
  <p>I want to dynamically build that query instead of specifying each field (it just seems cleaner this way.</p>