从nodejs的JsonObject中获取对象数组?

时间:2019-02-28 09:59:45

标签: javascript node.js json loopback

我想使用node.js中的联接从多个表中获取数据。我已经写了这个查询。

    Report.find({
  where: {id: data},
  include: {
    relation: 'reportSections',
    scope: {
      'fields': ['id', 'reportId', 'sectionId'],
      include: {
        relation: 'section',
        scope: {
          include: {
            relation: 'sectionFields',
            scope: {
              include: [{relation: 'reportMeta'},
                {relation: 'fields'}],
            },
          },
        },
      },
    },
  },
}, function(err, response) {

  console.log(response[0]);
  cb(null, Object.keys(response[0]));
});

此查询的结果是我收到此响应。

[
      {
        "Name": "Dalton Patron",
        "id": 1,
        "reportSections": [
          {
            "id": 1,
            "sectionId": 1,
            "reportId": 1,
            "section": {
              "label": "Personal Details",
              "id": 1,
              "sectionFields": [
                {
                  "label": "First Name",
                  "id": 1,
                  "sectionId": 1,
                  "fieldsId": 1,
                  "fields": {
                    "Type": "Text Field ",
                    "id": 1
                  },
                  "reportMeta": [
                    {
                      "Value": "Muhammad Naeem AKhtar",
                      "id": 1,
                      "sectionFieldId": 1
                    },
                    {
                      "Value": "sss",
                      "id": 2,
                      "sectionFieldId": 1
                    }
                  ]
                },
                {
                  "label": "Second Name",
                  "id": 2,
                  "sectionId": 1,
                  "fieldsId": 1,
                  "fields": {
                    "Type": "Text Field ",
                    "id": 1
                  },
                  "reportMeta": []
                }
              ]
            }
          }
        ]
      }
    ]

我想分别获取上述json中存在的每个对象。我在这里面临的问题是,如果我想在单独的变量中获取Name和Id属性,则可以成功获取该属性,但无法获取reportsections数组。您能告诉我如何使用Node进行获取吗?

如果我使用以下方法获得对象键:-

Object.keys(response[0]

正在返回:

[
  "__cachedRelations",
  "__data",
  "__dataSource",
  "__strict",
  "__persisted"
]

可以请有人让我知道节点js中有关此问题的解决方案。谢谢

0 个答案:

没有答案