Mongoose不接受以0开头的数字

Mongoose不接受以0开头的数字,mongoose,mongoose-schema,Mongoose,Mongoose Schema,我试着保存一个法国号码电话,例如:060908 这是我的身体要求: newAppointment.phone = req.body.phone; 我的模式: phone: { type:Number, required: true } 我的数据是:06 这将返回一个错误:语法错误:意外数字。当我将其转换为字符串时,它将删除0 回答:使用STRING存储电话,如0609084542您不能使用号码字段。改用字符串字段 phone:

我试着保存一个法国号码电话,例如:060908

这是我的身体要求:

newAppointment.phone = req.body.phone;
我的模式:

phone: {
            type:Number,
            required: true
        }
我的数据是:06

这将返回一个错误:语法错误:意外数字。当我将其转换为字符串时,它将删除0


回答:使用STRING

存储电话,如0609084542您不能使用
号码
字段。改用
字符串
字段

phone: {
      type: String,
      required: true
}

我想您需要一些关于类型的帮助(字符串、数字aka Int/Char/Long/Float/Double)

你有一篇关于堆栈溢出的帖子,内容是关于数字中的前导0。比如:

const toto = 0652;

为人所知

const toto = 0652;
不同于

const toto = 652;
const toto = '0652';
不同于

const toto = 652;
const toto = '0652';

如果您希望在第一位使用0。您必须在架构中将电话类型作为字符串,因为我们知道将0放在第一位不会更改号码的值

因此,您的模式如下所示:

phone: {
      type: String,
      required: true
}
然后将您的数据发送为“0609084542”