Search code examples
mongoosemongoose-schema

Mongoose not accept number begin with 0


I try to save a french number phone, ex : 060908...

This is my body request :

newAppointment.phone = req.body.phone;

My schema :

phone: {
            type:Number,
            required: true
        }

and my data : 06

This return me an error : SyntaxError: Unexpected number. When i convert it to string it's remove the 0.

Answer : Use STRING.


Solution

  • To store a phone like 0609084542 you cannot use a Number field. Use a String field instead.

    phone: {
          type: String,
          required: true
    }
    

    I think you need some help about types (String, Number aka Int/Char/Long/Float/Double).

    Here you have a stack overflow post speaking about the leading 0 in numbers. Like:

    const toto = 0652;
    

    To be known:

    const toto = 0652;
    

    is different than

    const toto = 652;
    

    is different than

    const toto = '0652';