-1
this.http.post(this.url + "/DeptWiseCount", {
  startDate: this.newStartDate,
  endDate: this.newEndDate
}).subscribe(
  data => (
    this.response = JSON.stringify(data),
    console.log(this.response + data[0]["port_count"])
  ),
  error => (
    this.handleError(error)
  )
)

In the above code I am simply want to get data from the API in my console log but it throw an error i.e. Cannot read properties of undefined (reading 'port_count'). I don't know why? Please help me. API output as mention below:

{
  "data": [
    {
      "key": "Pending",
      "total_count": 576871,
      "traffic": 2,
      "traf_count": 502045,
      "port": 1,
      "port_count": 74826
    },
    {
      "key": "Cash",
      "total_count": 305414,
      "traffic": 2,
      "traf_count": 245316,
      "port": 1,
      "port_count": 60098
    }
  ]
}

Thank You

Klint
  • 114
  • 2
  • 10

1 Answers1

0

JSON.stringify() is used to convert an object into json string. In your case you shoud be using JSON.parse() to generate a js object from a json response. Hope this helps