2

My fields are.

{
    "_id" : ObjectId("5b7f93224fc3b140983ea775"),
    "city" : "visakapatnam",
    "area" : "mvp",
    "zone" : "zone II",
    "ward" : "ward I",
    "status" : false,
    "createdAt" : ISODate("2018-08-24T05:09:54.279Z"),
    "updatedAt" : ISODate("2018-08-24T08:44:52.736Z"),
    "__v" : 0
}

every records defult field is status:false beased on id how to update status:true my requirement is used on postman http://localhost:3000/notes/5b7f96454fc3b140983ea775 this id status how to update true iam wrote findOne api is fine see this findOne api..any one please suggest update api.

 exports.findOne = function(req, res) {
    // Find a single note with a noteId
    Complaint.findById(req.params.noteId, function(err, note) {
        if(err) {
            console.log(err);
            if(err.kind === 'ObjectId') {
                return res.status(404).send({message: "Note not found with id " + req.params.noteId});                
            }
            return res.status(500).send({message: "Error retrieving note with id " + req.params.noteId});
        } 

        if(!note) {
            return res.status(404).send({message: "Note not found with id " + req.params.noteId});            
        }

        res.send(note);
    });
},

2 Answers 2

1

Update your code like below example:

app.post('/api/todo', function(req, res) {

    if (req.body.id) {
        Todos.findByIdAndUpdate(req.body.id, { todo: req.body.todo, isDone: req.body.isDone }, function(err, todo) {
            if (err) throw err;

            res.send('Success');
        });
    }
});

I hope it will work for you.

Sign up to request clarification or add additional context in comments.

Comments

0

Try in this way

app.patch('/todos/:id', (req, res) => {
  var id = req.params.id;
  var body = _.pick(req.body, ['text', 'completed']);

  if (!ObjectID.isValid(id)) {
    return res.status(404).send();
  }



  Todo.findByIdAndUpdate(id, {$set: body}, {new: true}).then((todo) => {
    if (!todo) {
      return res.status(404).send();
    }

    res.send({todo});
  }).catch((e) => {
    res.status(400).send();
  })
});

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.