【问题标题】:Keystone JS , what field type we can you on saving an array of objectsKeystone JS,我们可以在保存对象数组时使用什么字段类型
【发布时间】:2020-01-14 02:47:04
【问题描述】:

在我的程序中,我生成了一个对象数组,即 ImageData ,现在我想将该数据保存到 mongo Db ,除了 ImageData 键之外,我对其他键没有任何问题,因为我不知道要使用什么字段类型能够插入那些下面是对象数组的数据。我们可以使用 keystone 中的什么字段类型来保存下面的对象数组示例?谢谢。

数据模型,这是模型,我尝试使用文本数组但它似乎不起作用。

Data.add({
    name: { type: String, required: false },
    Type: { type: Types.Select, options: 'New, Used,', index: true },
    ImageData: { type: Types.TextArray },
    content: {
        brief: { type: Types.Html, wysiwyg: true, height: 150 },
        extended: { type: Types.Html, wysiwyg: true, height: 400 },
    },
});

我需要保存的 JSON 数据

  ImageData :  [ 
       { 
          "Uri":"Test.com",
          "Hash":"42e04950d6f11cd5350e3179083c7c7f",
          "Path":"/public/server/img/de29d68ab3594032bef70ead0b0d8fc2.jpg"
       },
       { 
          "Uri":"Test.com",
          "Hash":"42e04950d6f11cd5350e3179083c7c7f",
          "Path":"/public/server/img/de29d68ab3594032bef70ead0b0d8fc2.jpg"
       }
    ]

【问题讨论】:

  • 你试过ImageData: [{ Uri: String, Hash: String, Path: String }],吗?
  • 必须使用类型函数@chridam 指定字段
  • 它不起作用@chridam
  • 您的示例值是对象数组,而不是字符串数组。假设您使用的是 Keystone 4,则对象数组没有内置类型。
  • 所以我们模型中的字段类型是 Sir ?以便我们可以保存该对象数组?

标签: keystonejs


【解决方案1】:

抱歉这么晚才回复。

你可以试试下面的结构

Data.add({
  name: { type: String, required: false },
  Type: { type: Types.Select, options: "New, Used,", index: true },
  ImageData: { type: Types.Relationship, ref: "ImageObject", many: true }
});

ImageObject.add({
  Uri: { type: String },
  Hash: { type: String },
  Path: { type: String }
});

这里,many:true 很重要,您的 ImageData 将是一个数组

【讨论】:

    猜你喜欢
    • 2020-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-20
    • 1970-01-01
    相关资源
    最近更新 更多