2

I'm trying to access some data inside a json object and i'm somewhat confused with the issue i'm facing. I have a json object like the following

{
"status": "success",
"data": {
"image": null,
"video": null,
"author": null,
"publisher": "Microlink Documentation",
"title": "React | Microlink Documentation",
"date": null,
"lang": "en",
"description": null,
"logo": "https://microlink.io/favicon.ico",
"url": "https://docs.microlink.io/sdk/getting-started/react/"
}
}

Here i have wrote the code so that console.log("meta",metaData.data); logs out

"image": null,
"video": null,
"author": null,
"publisher": "Microlink Documentation",
"title": "React | Microlink Documentation",
"date": null,
"lang": "en",
"description": null,
"logo": "https://microlink.io/favicon.ico",
"url": "https://docs.microlink.io/sdk/getting-started/react/"

But i want to take the title separately. So i've used console.log("meta",metaData.data.title); But i'm getting a error saying

Uncaught (in promise) TypeError: Cannot read property 'title' of undefined

What seems to be the problem here?

3
  • It should work. Can you provide the code written above this JSON object? Commented Apr 24, 2018 at 11:05
  • console.log("meta",metaData.data); should include the word meta at the begining of the output but I can't see it in the output you've provided. Are you sure that the second printout is from that call to console.log? Commented Apr 24, 2018 at 11:10
  • As @Jayesh mentioned, there is nothing obviously wrong with what you have shown in your question. Can you post more of the contextual code? It is not obvious for example if the two console.log statements are truly in the same context. Commented Apr 24, 2018 at 19:48

1 Answer 1

2

What you are doing is absolutely right but should be wrapped in the condition like

if(metaData.data){
console.log(metaData.data.title);
}

because you are getting the data from an async call

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.