MongoDB: Document failed validation

 const articleSchema={
  title:String,
  content:String
};
const Article=mongoose.model("Article",articleSchema);

app.get("/articles",function(req,res){
   Article.find().then(function(foundarticles){
    res.send(foundarticles);
  }).catch(function(err){
    console.log(err);
  });
});

app.post("/articles",function(req,res){
  console.log(req.body.title);
  console.log(req.body.content);
  const newArticle=new Article({
    title:req.body.title,
    content:req.body.content
  });
  newArticle.save();
});

Hey @Hassan_Subhani,

Welcome to the MongoDB Community!

This error typically occurs when you try to save/insert a document that does not match the validation rules defined in your Mongoose schema.

Some common reasons why this error happens:

  • Required field is missing in the document being saved. For example, if the title is defined as required in the schema but the document being saved is missing the title.
  • Type validation fails. For example, the schema defines a field as a Number but you try to save a string value to that field.
  • If custom validation defined in the schema fails. Could you confirm if you have any custom validation setups?

Please make sure the document being saved has all required fields populated.

In case of any further help, please share the error log message you are encountering and the workflow of posting the data via API. This will help us to assist you better.

Regards,
Kushagra