【问题标题】:Mongoose not accept number begin with 0Mongoose 不接受以 0 开头的数字
【发布时间】:2016-11-10 01:26:20
【问题描述】:

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

这是我的身体要求:

newAppointment.phone = req.body.phone;

我的架构:

phone: {
            type:Number,
            required: true
        }

和我的数据:06

这返回一个错误:SyntaxError: Unexpected number。当我将它转换为字符串时,它是 remove 0.

答案:使用 STRING

【问题讨论】:

    标签: mongoose mongoose-schema


    【解决方案1】:

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

    所以,你的架构是这样的:

    phone: {
          type: String,
          required: true
    }
    

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

    【讨论】:

      【解决方案2】:

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

      phone: {
            type: String,
            required: true
      }
      

      我认为您需要一些关于类型的帮助(String、Number aka Int/Char/Long/Float/Double)。

      Here 你有一个堆栈溢出帖子谈到数字中领先的 0。喜欢:

      const toto = 0652;
      

      未知

      const toto = 0652;
      

      不同于

      const toto = 652;
      

      不同于

      const toto = '0652';
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-06
        • 2011-06-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多