Monday, 13 March 2017

MongoDB findAndModify won't update - Node.js

I'm working in Node.js with a MongoDB database. I'm trying to utilize findAndModify with user data from an ejs page.

Here's my ejs code:

<div data-role="main" class="ui-content">
  <a href="#modPop" data-rel="popup" class="ui-btn ui-btn-inline ui-corner-all ui-icon-check ui-btn-icon-left">Modify Item</a>

<div data-role="popup" id="modPop" class="ui-content" style="min-width:250px;">
<form action="/modify" method="POST">
  Item <input type="text" placeholder="item" name="item"><br>
  Quantity <input type="number" name="quantity"><br>
  Date <input type="date" name="date"><br>
  <button type="submit">Submit</button>
</form>
</div>
</div>

And here is my linked code in server.js:

app.post('/modify', (req, res) => {
console.log('got Post request');
  console.log(req.body);

  db.collection('Inventory').findAndModify(
    {_item: req.body.item} ,
    [],
    { $inc: {_quantity: Number(req.body.quantity)}, $set: {_date: req.body.date}},
    {new: true},
    (err, result) => {
      if (err) {
            return console.log(err);
        }
      console.log('saved to database');
      res.redirect('/shiftleader');
    })

});

Whenever I test this, the update doesn't process. I don't get an error, my terminal output looks like it's going through properly, but it just doesn't update the database.



via Mitchell Tague

No comments:

Post a Comment