Validation in Update and errors

RULES DEFINED ARE VALIDATED ONLY DURING INSERTION.

WHILE UPDATING THEY ARE NOT VALIDATED

price: {
    type: Number,
    min: 1,

  } 


Book.findByIdAndUpdate("67c1fd5b36e4d6da65845b82", { price: -100 })
  .then((res) => {
    console.log(res);
  })
  .catch((err) => {
    console.log(err);
  });



TO OVERCOME THIS 


Book.findByIdAndUpdate("67c1fd5b36e4d6da65845b82", { price: -100 },{runValidators:true})
  .then((res) => {
    console.log(res);
  })
  .catch((err) => {
    console.log(err);
  });


WE CAN DEFINE CUSTOM ERRORS

print the errors that we define

  price: {
    type: Number,
    min: [1, "Price is too low to afford the book"],
  },








Comments

Popular posts from this blog

DELETE ROUTE

CREATE ROUTE

Schema type options