【问题标题】:Nested Model in Loopback APILoopback API 中的嵌套模型
【发布时间】:2019-06-05 16:01:20
【问题描述】:

我正在使用 Loopback 创建 API,并创建了一个模型。其中一个属性是“深度”,在其中,它应该有 4 个其他数字属性。所以我将 depth 设为了一个数字数组,但在我的 fish.json 文件中看起来像这样:

"depth": {
      "type": [
        "number"
      ],
      "required": true
    },

这是整个文件(fish.json):

{
  "name": "Fish",
  "plural": "fish",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "name": {
      "type": "string",
      "required": true
    },
    "scientific": {
      "type": "string",
      "required": true
    },
    "environment": {
      "type": "string",
      "required": true
    },
    "minClimate": {
      "type": "number",
      "required": true
    },
    "maxClimate": {
      "type": "number",
      "required": true
    },
    "depth": {
      "type": [
        "number"
      ],
      "required": true
    },
    "avLength": {
      "type": "number",
      "required": true
    },
    "maxLength": {
      "type": "number",
      "required": true
    },
    "avWeight": {
      "type": "number",
      "required": true
    },
    "maxWeight": {
      "type": "number",
      "required": true
    },
    "maxAge": {
      "type": "number",
      "required": true
    },
    "description": {
      "type": "string",
      "required": true
    },
    "imageUrl": {
      "type": "string",
      "required": true
    }
  },
  "validations": [],
  "relations": {},
  "acls": [],
  "methods": {}
}

我查看了 Loopback 文档,看起来我可以在这里使用嵌套模型。我将如何将此数组更改为嵌套模型?我是否需要创建一个新模型,然后以某种方式将该新模型添加到深度属性中?

基本上,我希望 JSON 看起来像这样:

"depth": {
      "min": "0",
      "max": "0",
      "avMin": "0",
      "avMax": "0"
    },

【问题讨论】:

    标签: json api model nested loopback


    【解决方案1】:

    你可以试试这个:-

    "depth": {
          "min": {"type": "number"},
          "max": {"type": "number"},
          "avMin": {"type": "number"},
          "avMax": {"type": "number"}
        }
    

    【讨论】:

      猜你喜欢
      • 2016-04-15
      • 2018-07-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 2016-10-09
      • 2017-07-06
      相关资源
      最近更新 更多