0

Trying to update the below M Lab hosted mongo DB, not much luck, any ideas? Form input via handlebars template, routed to express server. No errors, and DB does not update.

{ "_id": { "$oid": "58fdec20a8aac60a190152ae" }, "name": "Item1", "status": 0 }

app.put("/:id", function(req, res) {

    db.collection.update({_id: req.body.id}, 
        {
            $set:{status: 1}, function(){
            res.redirect("/");
            console.log('updated')
            }
    });

}); 
<ul> Let's Eat
  {{#each ndev}}
    <li> 
      <p>
        <form action="/:{{this._id}}?_method=PUT" method="POST">
          <input type="hidden" name="id" value="{{_id}}">{{name}}
          <button type="submit">Update</button>
        </form>
      </p>
    </li>
  {{/each}}
</ul>

3
  • Use findOneAndUpdate method, also pass {new: true} to get back updated document Commented Apr 25, 2017 at 2:46
  • Thank you for the feedback, you got me on the right track. Commented Apr 25, 2017 at 3:32
  • Don't use findAndModify you have the _id and there would be only one document in database. Use findOneAndUpdate method Commented Apr 25, 2017 at 3:50

2 Answers 2

1

Please make callback function out of $set braces just like this:

app.put("/:id", function(req, res) {
    db.collection.update({_id: req.body.id},{$set:{status: 1}}, function(err,doc)
    {
      if(err){
         console.log(err);
      }
      res.redirect("/");
      console.log('updated')        
    });
}); 
Sign up to request clarification or add additional context in comments.

Comments

0

Using mongojs below was my solution.

app.put("/:id", function(req, res) {
    var id = req.body.id; 
    db.collection.findAndModify({
        query: { _id: mongojs.ObjectId(id)}, 
        update: { $set: {status: 1}},
        new: true},
        function(err, doc, collection){
        if(err){
            res.send(err);
            }else{
        res.redirect("/");
            }
            });
        });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.