【问题标题】:Unable to get nested array of objects from json-server无法从 json-server 获取嵌套的对象数组
【发布时间】:2021-09-23 10:27:15
【问题描述】:

我正在尝试通过 json-server 从嵌套的 JSON 属性 fields 之一获取对象数组,但出现 404 错误。

当我在浏览器“http://localhost:4001/templates/5f105f243c076f3a82c95353”中访问 URL 时,我得到了 db.json 中提到的 JSON,但是当我尝试访问此 URL 时“http://localhost” :4001/templates/5f105f243c076f3a82c95353/fields' 我收到 404 错误。

我相信来自db.json 文件,因为fields 数组是模板的一部分,所以/fields 后跟特定模板的基本URL (http://localhost:4001/templates/5f105f243c076f3a82c95353) 应该可以工作但它不起作用,请帮助我。

db.json 文件

{
  "id": "5f105f243c076f3a82c95353",
  "templateIdName": "DNIS Groups",
  "templateDisplayName": "DNIS Groups Template",
  "templateDescription": "Template for DNIS Groups",
  "fieldCounter": 2,
  "fields": [
    {
      "id": 1,
      "fieldIdName": "OutageGroup",
      "fieldDisplayName": "Outage Group",
      "description": "Outage Group",
      "type": "MultiList",
      "values": [
        {
          "listValue": "2803",
          "listDisplay": "Wire Down"
        },
        {
          "listValue": "2804",
          "listDisplay": "Streetlight Outage"
        }
      ],
      "initialValue": "2803",
      "lockValue": "false",
      "requireValue": "true",
      "uniqueField": "true",
      "autoFilter": "false",
      "hideInList": "false",
      "indexField": "true"
    },
    {
      "id": 2,
      "fieldIdName": "ProtectLinesGroups",
      "fieldDisplayName": "Protect Lines Group",
      "description": "Protect Lines Group",
      "type": "MultiList",
      "values": [
        {
          "listValue": "2814",
          "listDisplay": "Cover Lines"
        },
        {
          "listValue": "2815",
          "listDisplay": "Voltage"
        }
      ],
      "initialValue": "2814",
      "lockValue": "false",
      "requireValue": "true",
      "uniqueField": "false",
      "autoFilter": "false",
      "hideInList": "false",
      "indexField": "false"
    }
  ]
}

routes.json 文件

{
  "/webservice/:clientId/dblookup/template": "/templates",
  "/webservice/:clientid/dblookup/template/:id": "/templates/:id",
  "/webservice/:clientid/dblookup/template/:id/fields": "/templates/:id/fields"
}

【问题讨论】:

  • 嗨 Siddesh,您找到问题的答案了吗?
  • @Varun 是的,实际上 JSON-Server 没有访问嵌套资源的规定,它只能访问文件中定义为顶级属性的资源,在本例中为顶部level 属性将为templates,我可以使用http://localhost:4001/templates 访问它,但我无法在此处访问嵌套属性values

标签: javascript json xmlhttprequest json-server


【解决方案1】:

实际上 JSON-Server 没有访问嵌套资源的规定,它只能访问文件中定义为顶级属性的资源,在这个例子中,顶级属性将是 templates 和我可以使用http://localhost:4001/templates 访问它,但我无法访问嵌套属性values,因为http://localhost:4001/templates/2/values 它给出了404 not found 错误。

【讨论】:

    猜你喜欢
    • 2020-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-10
    • 2019-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多