Mongoose 不接受以 0 开头的数字

Mongoose not accept number begin with 0

我尝试保存一个法国号码 phone,例如:060908...

这是我的 body 请求:

newAppointment.phone = req.body.phone;

我的架构:

phone: {
            type:Number,
            required: true
        }

我的数据:06

这 return 给我一个错误:SyntaxError: 意外数字。当我将它转换为字符串时,它是 remove the 0.

答案:使用 STRING.

要存储像 0609084542 这样的 phone,您不能使用 Number 字段。请改用 String 字段。

phone: {
      type: String,
      required: true
}

我想你需要一些关于类型的帮助(字符串,数字又名 Int/Char/Long/Float/Double)。

你有一个堆栈溢出 post 谈论领先的 0 数字。喜欢:

const toto = 0652;

已知:

const toto = 0652;

不同于

const toto = 652;

不同于

const toto = '0652';

如果你想要0在第一位。您必须在架构中将 phone 类型作为字符串,因为我们知道将 0 放在首位不会更改数字的值

因此,您的架构如下:

phone: {
      type: String,
      required: true
}

然后将您的数据发送为“0609084542”