0

对不起咖啡脚本。

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

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 对象中的键/值对并分配: attr: Schema.Types.Mixed

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

任何帮助将不胜感激。

4

1 回答 1

2

The type property of attr is likely tripping up Mongoose to thinking attr is a string instead of an embedded object. Use a more explicit definition for the property, like this:

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-06-28T05:39:11.890 回答