Monday, 17 June 2019

Mongoose save method saves multiple times same document

Hi I have one input field date selector on angular UI. I select the date from field and update the starting Date and till there it works correctly. but If I select the date again it duplicates the document instead of updating it.

working code:

router.post('/request/waiting/:startingDate', (req, res) => {
  const waiting = new WaitingRequest({
    startingDate: req.params.startingDate,
    requestID: req.body.requestID,
    contactInformation: req.body.contactInformation,
    orderInformation: req.body.orderInformation,
    requestInformation: req.body.requestInformation,
    installations: req.body.installations
  });
  waiting.save().then(result => {
    return res.status(201).json({
      message: 'Updated and Saved /waiting',
      result: result
    });
  });
});

what I have tried so far but it returns null:

router.post('/request/waiting/:startingDate', (req, res) => {
  WaitingRequest.findOneAndUpdate({
    requestID: req.body.requestID
  }, {
    startingDate: req.params.startingDate
  }).then(result => {
    console.log(result)
    if (result) {
      return res.status(201).json({
        message: 'Updated and saved',
        result: result
      });
    } else {
      return res.status(404).json({
        message: "404 Request Not Found"
      })
    }
  })
});



from Mongoose save method saves multiple times same document

No comments:

Post a Comment