【问题标题】:Mongoose-How to restrict date to be bigger than today´s date? [duplicate]Mongoose-如何将日期限制为大于今天的日期? [复制]
【发布时间】:2020-08-28 22:09:06
【问题描述】:

对于这个模型,我想防止用户将日期设置在今天的日期之前。我该怎么做?

var requestSchema = new Schema({
  paciente: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "User",
  },
  encaminhado: {
    type: Boolean,
    required: [true, "encaminhado is a required field"],
  }, //vem do body
  pessoaRisco: {
    type: Boolean,
    required: [true, "pessoaRisco is a required field"],
  }, //vem do body
  trabalhoRisco: {
    type: Boolean,
    required: [true, "trabalhoRisco is a required field"],
  }, //vem do body
  estadoPedido: {
    type: String,
    enum: ["Pendente", "Agendado", "Concluído", "Aguarda Resultado"],
  },
  resultado: { type: String, enum: ["Positivo", "Negativo"] },
  dataExame: { type: Date }, //data tem de ser superior a data atual
});

【问题讨论】:

    标签: javascript node.js mongodb mongoose model


    【解决方案1】:

    您可以使用验证功能,例如:

    dataExame: { 
        type: Date,
        validate: function(input) {
            /* return true only if the input is a valid date, AND is 
            greater than or equal to the current date/time */
            return typeof new Date(input) === 'date' && new Date(input) >= new Date();
        },
        message: input => `${input} must be greater than or equal to the current date!`
    }, //data tem de ser superior a data atual
    

    【讨论】:

    • @R Greenstreet 即使拥有出色的数据,我也无法创建
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-18
    • 1970-01-01
    • 2016-03-06
    • 1970-01-01
    • 2018-01-31
    • 2015-08-03
    相关资源
    最近更新 更多