• 4
Votes
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /var/www/html/cnasolution/site/application/views/question.php
Line: 191
Function: _error_handler

File: /var/www/html/cnasolution/site/application/controllers/Questions.php
Line: 419
Function: view

File: /var/www/html/cnasolution/site/index.php
Line: 315
Function: require_once

Moral of the story: Always double check your keys. My key was states and was trying to access state

The Server is using Flask and responds a nested JSON. On the front-end, I can see that the request went through and I can console.log the JSON just fine.

If I run: Object.keys(JSON) it prints all the first keys.

Similarly, if I print out the JSON it will print it out as a JSON.

I have checked to make sure that the response is received a JSON, by using typeof().

Whenever I try to access a value in the JSON using an existing key, counties for example, it won't work.

If you notice below, counties.cases exists but when I try to access JSON.counties.cases it complains that counties doesn't exist. I have tried every single key.

And have tried to iterate through the JSON and every iteration returns undefined.

I have also tried JSON.parse() and it doesn't parse because it is already a JSON object.

JSON Error

    send_request = async () => {         const host = window.location.protocol + '//' + window.location.hostname + ":8080"         const url: string = `${host}/get-all-data`         let response = await fetch(             url, {mode: 'cors'}         ).then(response => response.json().then(res => {             console.log(res) // Works             console.log(res.state) // Doesn't work             console.log(res.state.cases) // Doesn't work } 
Download script demo [LINK] [Origin]
Download script demo [LINK 2] [Onedrive] Download script demo [LINK 2] [Google drive]

It sounds like you are trying to access an object that it is not there yet. I mean, I think there is a Promise not resolved yet at the momento you are accessing the fields.

And at the time you open the object in the inspector, the promise is resolved and the result is there.

Whatever you are using to do the request in the browser, I presume it is returning that Promise and you need to wait for it to resolve before accessing the result.

Assuming you have your response promise in a variable p, you are able to wait for it doing something like:

p.then(function (response) {   console.log(response.countries); }); 

If you need more details please let me know, or add some other info of your specific case.

see demo
  • 15
Reply Report