0

console.log(result.data[0]);

returns:

{ name: 'page_stories',
  period: 'day',
  values: 
  [ { value: 114, end_time: '2016-07-31T07:00:00+0000' },
    { value: 68, end_time: '2016-08-01T07:00:00+0000' },
    { value: 134, end_time: '2016-08-02T07:00:00+0000' } ],
  title: 'Daily Page Stories',
  description: 'Daily: The number of stories created about your Page. (Total Count)',
  id: '462582393917692/insights/page_stories/day' }

but for some reason var name = result.data[0].name; is returning:

TypeError: Cannot read property 'name' of undefined

I can't for the life of me figure out why. the 'name' property clearly exists as does the result.data[0] object. Any help would be fantastic.

edit: found out that the data format wasn't uniform for every single request (just most!). Thanks for the responses. I should've made it clear that I was feeding batch requests through this code too.

4
  • 5
    You need to provide a complete minimal reproducible example
    – Quentin
    Aug 4, 2016 at 10:52
  • result is parsed or only string? JSON.parse(result.data[0]); ?
    – Dave
    Aug 4, 2016 at 10:53
  • 2
    I'm betting you're trying to access it before an async function completes or something. Are you setting the value in a callback?
    – david
    Aug 4, 2016 at 10:53
  • thanks for the comments. Having just double checked my code it looks like the data format is different in some of the batch requests I'm feeding through this. I'll close the question off.
    – Ed Shee
    Aug 4, 2016 at 10:57

1 Answer 1

-2

Ensure that you are actually trying to access an object and not a string. JSON.parse(obj) if needed.

3
  • But if result.data[0] is undefined how come he has it logged, pay attention to the first line of the question.
    – Gabrielo
    Aug 4, 2016 at 11:13
  • I read that. But we do not know what happens in between and we do not know whether the code is executed synchronously or asynchronously. And the error message clearly tells you that result.data[0] is undefined.
    – str
    Aug 4, 2016 at 11:18
  • This answer, although may not directly be answering the question, may be relevant to others experiencing a similar issue where a textual representation suggests the property exists but these properties are seemingly undefined. Helped me. Thanks. May 19, 2020 at 7:13

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.