Search code examples
javascriptjsonxmlhttprequestjson-server

Unable to get nested array of objects from json-server


I am trying to get array of objects from one of the nested JSON properties fields through the json-server, but I am getting 404 error.

When I access the URL in the browser 'http://localhost:4001/templates/5f105f243c076f3a82c95353' I get the JSON as mentioned in the db.json, but when I try to access this URL 'http://localhost:4001/templates/5f105f243c076f3a82c95353/fields' I am getting 404 error.

I believe from the db.json file, as fields array is a part of template so /fields followed by the base URL of the particular template (http://localhost:4001/templates/5f105f243c076f3a82c95353) should work but it is not working, please help me.

db.json file

{
  "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 file

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


Solution

  • Actually JSON-Server don't have the provision to access the nested resources, it can only access resources which are defined as the top level properties in the file, in this example the top level property will be templates and I can access that using http://localhost:4001/templates but I cannot access the nested property values here as http://localhost:4001/templates/2/values it gives 404 not found error.