skip to Main Content

I have a mongodb schema like below.

const DemoSchema: Schema = new Schema({
  demoProperty1: { type: String, required: true }
});

I create a document with syntax in node js server like below.

const savedDemo = await this.demoModel.create({demoProperty1:""});

Why it is returning error message

UnhandledPromiseRejectionWarning: ValidationError:

How to fix this issue?

2

Answers


  1. Chosen as BEST ANSWER

    Yes I have fix this issue by trial and error. Empty string is not eligible for mongodb required schema, Took me 5 hours to figure it out. I can just passed in any string but not an empty string to make it valid and passed without any error.


  2. From the documentation:

    // Use this to allow empty strings to pass the `required` validator
    mongoose.Schema.Types.String.checkRequired(v => typeof v === 'string');
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search