简体   繁体   中英

How do I access an object in an array from an API hosted by json-server

I'm using json-server for hosting an API in my project.

What json-server gives me is http://localhost:3000/records .

{
    "record": [
        {
            "userID": "2",
            "userFirstName": "Arya",
            "userLastName": "Stark",
            "userMiddleName": "null",
            "roleName": "Admin",
            "roleID": "1"
        },
        {
            "userID": "1",
            "userFirstName": "John",
            "userLastName": "Snow",
            "userMiddleName": "null",
            "roleName": "Moderator",
            "roleID": "1"
        }
    ]
}

I'm using Postman for making GET request to the server.

How do I go into record array and access the object with userID as 2 ?

I tried this way but it's returning an empty object.

Please correct me if I'm wrong:

http://localhost:3000/records/record?userID=2

My actual JSON object file:

{
  "records": {
    "record": [
      {
        "userID": "2",
        "userFirstName": "Arya",
        "userLastName": "Stark",
        "userMiddleName": "null",
        "roleName": "Admin",
        "roleID": "1"
      },
      {
        "userID": "1",
        "userFirstName": "John",
        "userLastName": "Snow",
        "userMiddleName": "null",
        "roleName": "Moderator",
        "roleID": "1"
      }
    ]
  }
}

Just so slightly change your JSON structure:

{
  "records": [
    {
      "userID": "2",
      "userFirstName": "Arya",
      "userLastName": "Stark",
      "userMiddleName": "null",
      "roleName": "Admin",
      "roleID": "1"
    },
    {
      "userID": "1",
      "userFirstName": "John",
      "userLastName": "Snow",
      "userMiddleName": "null",
      "roleName": "Moderator",
      "roleID": "1"
    }
  ]
}

Then http://localhost:3000/records?userID=2 returns

[
  {
    "userID": "2",
    "userFirstName": "Arya",
    "userLastName": "Stark",
    "userMiddleName": "null",
    "roleName": "Admin",
    "roleID": "1"
  }
]

Aside from this solving the actual access problem I was also unable to find a solution to filter based on object properties that are part of an array which is stored in a property of your db.json object.

Simple but have you tried just using /records/record/2 the json-server module takes most of the implementation away from the user and this may just work, as it's designed to be a quick REST API to get people started.

Check out the examples on the documentation page or take a look at the API running here http://jsonplaceholder.typicode.com/ as it's using the same code.

You have to change something on your server side to filter request according to the request parameters

Or just use javascript:

data.records.record.filter((user)=>user.userID==2)[0]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM