代码之家  ›  专栏  ›  技术社区  ›  TheWebs

环回2-定义模型上的对象数组

  •  3
  • TheWebs  · 技术社区  · 7 年前

    如果我有 usermodel

    "events": {
      "type": [
        "Object"
      ]
    },
    

    我需要在 usermodel.js [{name: 'sample', ...}, ...] 到用户表的事件列?

    我这样问是因为如果我从 .json findByid . 我的调试将其缩小到了这组特定的代码。

    2 回复  |  直到 7 年前
        1
  •  7
  •   Rohit Hazra    7 年前

    {
     "events":{
        "type": [
           {
              "key": "type",
              "key2": "type"
           }
        ]
     }
    }
    

    你可以看到一个。js示例 here 和json示例 here 但我也看到了实施中的一个问题 在这里

    这个模型有问题。当我们使用任何get调用获取数据时,它 数据正确保存在数据库中。

    我建议你自己试试,因为这在很大程度上取决于版本和驱动程序。

        2
  •  2
  •   YeeHaw1234    7 年前

    另一种方法是将数组中需要的对象定义为模型,然后使用该模型作为类型:

    型号:Class

    {
        "name": "Class",
        "base": "Model",
        "strict": true,
        "idInjection": false,
        "properties": {
            "label": {
                "type": "string",
                "required": true
            }
        }
    }
    

    {
        "name": "Student",
        "base": "PersistedModel",
        "strict": true,
        "idInjection": true,
        "properties": {
            "classes": {
                "type": ["Class"],
                "required": false
            },
        }
    }