【问题标题】:mongoose validate object structure猫鼬验证对象结构
【发布时间】:2018-06-23 02:21:22
【问题描述】:

我希望属性“localized_words”是一个对象映射字符串到array

使用下面的代码,我只能确保它是一个Object,但是如何确保值是严格数组

var schema = mongoose.Schema({
   // example
   // this should fail   : { en: [], fr: 123 }
   // this should succeed: { en: [], fr: []  }
   // but with current code, any object will pass validation
   localized_words : { type: Object }
});

我要存储的数据示例:{en: ['car', 'apple'], fr: ['voiture', 'pomme']}

【问题讨论】:

  • 能否包含您要存储的示例数据?
  • @Khang 刚刚做了

标签: node.js mongodb object mongoose


【解决方案1】:

这应该可行:

const schema = mongoose.Schema({
   localized_words : {
       en: [String],
       fr: [String],
   },
});

【讨论】:

    猜你喜欢
    • 2016-09-23
    • 1970-01-01
    • 2020-03-26
    • 1970-01-01
    • 1970-01-01
    • 2021-08-19
    • 2021-09-21
    • 1970-01-01
    • 2016-02-14
    相关资源
    最近更新 更多