【问题标题】:Mongoose ignoring part of object unless defined as Schema.Types.Mixed猫鼬忽略对象的一部分,除非定义为 Schema.Types.Mixed
【发布时间】:2013-06-25 17:55:56
【问题描述】:

抱歉,咖啡脚本。

用这个把我的头发拉出来。我有一个类似于这样的架构:

reviewSchema = Schema
  title: String
  meta: 
    author: String
    date: String
  article: String
  tags: [
    type: Schema.Types.ObjectId
    ref: "Tag"
  ]
  images: [
    type: Schema.Types.ObjectId
    ref: "Image"
  ]
  attr: 
    appearance: String
    aroma: String
    flavor: String
    from: String
    grade: String
    lineage: String
    name: String
    overall: String
    packaging: String
    pickupdate: String
    price: String
    reason: String
    story: String
    type: String
Review = mongoose.model 'Review', reviewSchema

我还有一些数据,我已经将它们组装成一些带有相应数据的 JSON 文件。当我去创建一个新架构时,我会查看该文件并获取 JSON 并通过以下方式创建一个新的评论对象:

thisReview = 新评论 json,最小化:false

如果我在创建 'thisReview' 之前 console.log json,我会看到我的 'attr' 键已正确填充数据,但如果我 console.log 'thisReview' 或检查数据库,我不会收到 ' attr' 我的文件的关键。 'attr' 被完全忽略。

我尽可能确保每个 JSON 文件的 JSON.attr 对象都有来自模式的每个键,对于那些不存在的键,它们都带有一个“”(空)字符串。其他嵌套对象(如模式的元对象)正在完美填充。

知道发生了什么吗?

额外信息:当我摆脱 attr 对象中的键/值对并分配: 属性:Schema.Types.Mixed

然后所有数据都成功保存,包括空 ("") 字符串。

任何帮助将不胜感激非常

【问题讨论】:

    标签: javascript node.js mongodb coffeescript mongoose


    【解决方案1】:

    attrtype 属性可能会让 Mongoose 误以为 attr 是字符串而不是嵌入对象。对属性使用更明确的定义,如下所示:

    attr: 
        appearance: String
        aroma: String
        flavor: String
        from: String
        grade: String
        lineage: String
        name: String
        overall: String
        packaging: String
        pickupdate: String
        price: String
        reason: String
        story: String
        type:
            type: String
    

    【讨论】:

    • 你是我今天最喜欢的人!我一直在寻找关键字,但我不知道我是如何忽略这一点的。非常感谢。
    猜你喜欢
    • 2013-05-08
    • 1970-01-01
    • 2016-11-06
    • 1970-01-01
    • 2013-11-27
    • 2019-03-21
    • 1970-01-01
    • 1970-01-01
    • 2020-06-28
    相关资源
    最近更新 更多