React Native- Error Accessing Fetched JSON data objects
I am very new to React Native, Networking, and Programming as a whole. I am attempting to fetch JSON data from Reddit for an app I'm building, but I am receiving error messages when trying to access the Objects of the data. Below is the code I am using to fetch and console log this data.
componentDidMount(){
fetch ('https://www.reddit.com/r/financialindependence.json')
.then(res => res.json())
.then(data => {
this.setState({
data: data
})
})
.catch( error => {
console.error(error)
})
}
render() {
const myData = this.state.data
if (myData !== undefined) {
console.log(myData)
}
return(
<View style = {styles.container}>
<MainDrawerHeader title = 'Links' />
<Text> This is the Links Screen </Text>
</View>
)
}
}
This works and logs to the console the following.
[16:54:41] Object {
[16:54:41] "data": Object {
[16:54:41] "after": "t3_9ysab7",
[16:54:41] "before": null,
[16:54:41] "children": Array [
[16:54:41] Object {
[16:54:41] "data": Object {
[16:54:41] "approved_at_utc": null,
[16:54:41] "approved_by": null,
[16:54:41] "archived": true,
[16:54:41] "author": "Omitted For Privacy",
And when I access myData.data , it works as well
[16:59:29] Object {
[16:59:29] "after": "t3_9ysab7",
[16:59:29] "before": null,
[16:59:29] "children": Array [
[16:59:29] Object {
[16:59:29] "data": Object {
[16:59:29] "approved_at_utc": null,
But when I try to access the second nested object (either 'after', 'before', or 'children' (and since children is an array, I tried to access the first element) I receive the following error:
[17:00:39] TypeError: TypeError: TypeError:
undefined is not an object (evaluating 'myData.data.after')
Which confuses me because I tried to catch the undefined object with my 'if' statement. Any tips, not sure where to go from here. Thank you
javascript json react-native networking javascript-objects
add a comment |
I am very new to React Native, Networking, and Programming as a whole. I am attempting to fetch JSON data from Reddit for an app I'm building, but I am receiving error messages when trying to access the Objects of the data. Below is the code I am using to fetch and console log this data.
componentDidMount(){
fetch ('https://www.reddit.com/r/financialindependence.json')
.then(res => res.json())
.then(data => {
this.setState({
data: data
})
})
.catch( error => {
console.error(error)
})
}
render() {
const myData = this.state.data
if (myData !== undefined) {
console.log(myData)
}
return(
<View style = {styles.container}>
<MainDrawerHeader title = 'Links' />
<Text> This is the Links Screen </Text>
</View>
)
}
}
This works and logs to the console the following.
[16:54:41] Object {
[16:54:41] "data": Object {
[16:54:41] "after": "t3_9ysab7",
[16:54:41] "before": null,
[16:54:41] "children": Array [
[16:54:41] Object {
[16:54:41] "data": Object {
[16:54:41] "approved_at_utc": null,
[16:54:41] "approved_by": null,
[16:54:41] "archived": true,
[16:54:41] "author": "Omitted For Privacy",
And when I access myData.data , it works as well
[16:59:29] Object {
[16:59:29] "after": "t3_9ysab7",
[16:59:29] "before": null,
[16:59:29] "children": Array [
[16:59:29] Object {
[16:59:29] "data": Object {
[16:59:29] "approved_at_utc": null,
But when I try to access the second nested object (either 'after', 'before', or 'children' (and since children is an array, I tried to access the first element) I receive the following error:
[17:00:39] TypeError: TypeError: TypeError:
undefined is not an object (evaluating 'myData.data.after')
Which confuses me because I tried to catch the undefined object with my 'if' statement. Any tips, not sure where to go from here. Thank you
javascript json react-native networking javascript-objects
before you call this.setState do a console.log(data) and post here a image of this please.
– Helmer Barcos
Nov 24 '18 at 2:06
Hi Helmer, thanks for responding. Here is the console.log before the setState. (Apologies, I don't know how to format comments): Before Set State: Object { [18:13:18] "data": Object { [18:13:18] "after": "t3_9ysab7", [18:13:18] "before": null, [18:13:18] "children": Array [ [18:13:18] Object { [18:13:18] "data": Object { [18:13:18] "approved_at_utc": null, [18:13:18] "approved_by": null, [18:13:18] "archived": true,
– holdenmaudlin
Nov 24 '18 at 2:14
but i mean a image...
– Helmer Barcos
Nov 24 '18 at 2:37
Here is the link to the console log. Sorry about that.
– holdenmaudlin
Nov 24 '18 at 7:17
add a comment |
I am very new to React Native, Networking, and Programming as a whole. I am attempting to fetch JSON data from Reddit for an app I'm building, but I am receiving error messages when trying to access the Objects of the data. Below is the code I am using to fetch and console log this data.
componentDidMount(){
fetch ('https://www.reddit.com/r/financialindependence.json')
.then(res => res.json())
.then(data => {
this.setState({
data: data
})
})
.catch( error => {
console.error(error)
})
}
render() {
const myData = this.state.data
if (myData !== undefined) {
console.log(myData)
}
return(
<View style = {styles.container}>
<MainDrawerHeader title = 'Links' />
<Text> This is the Links Screen </Text>
</View>
)
}
}
This works and logs to the console the following.
[16:54:41] Object {
[16:54:41] "data": Object {
[16:54:41] "after": "t3_9ysab7",
[16:54:41] "before": null,
[16:54:41] "children": Array [
[16:54:41] Object {
[16:54:41] "data": Object {
[16:54:41] "approved_at_utc": null,
[16:54:41] "approved_by": null,
[16:54:41] "archived": true,
[16:54:41] "author": "Omitted For Privacy",
And when I access myData.data , it works as well
[16:59:29] Object {
[16:59:29] "after": "t3_9ysab7",
[16:59:29] "before": null,
[16:59:29] "children": Array [
[16:59:29] Object {
[16:59:29] "data": Object {
[16:59:29] "approved_at_utc": null,
But when I try to access the second nested object (either 'after', 'before', or 'children' (and since children is an array, I tried to access the first element) I receive the following error:
[17:00:39] TypeError: TypeError: TypeError:
undefined is not an object (evaluating 'myData.data.after')
Which confuses me because I tried to catch the undefined object with my 'if' statement. Any tips, not sure where to go from here. Thank you
javascript json react-native networking javascript-objects
I am very new to React Native, Networking, and Programming as a whole. I am attempting to fetch JSON data from Reddit for an app I'm building, but I am receiving error messages when trying to access the Objects of the data. Below is the code I am using to fetch and console log this data.
componentDidMount(){
fetch ('https://www.reddit.com/r/financialindependence.json')
.then(res => res.json())
.then(data => {
this.setState({
data: data
})
})
.catch( error => {
console.error(error)
})
}
render() {
const myData = this.state.data
if (myData !== undefined) {
console.log(myData)
}
return(
<View style = {styles.container}>
<MainDrawerHeader title = 'Links' />
<Text> This is the Links Screen </Text>
</View>
)
}
}
This works and logs to the console the following.
[16:54:41] Object {
[16:54:41] "data": Object {
[16:54:41] "after": "t3_9ysab7",
[16:54:41] "before": null,
[16:54:41] "children": Array [
[16:54:41] Object {
[16:54:41] "data": Object {
[16:54:41] "approved_at_utc": null,
[16:54:41] "approved_by": null,
[16:54:41] "archived": true,
[16:54:41] "author": "Omitted For Privacy",
And when I access myData.data , it works as well
[16:59:29] Object {
[16:59:29] "after": "t3_9ysab7",
[16:59:29] "before": null,
[16:59:29] "children": Array [
[16:59:29] Object {
[16:59:29] "data": Object {
[16:59:29] "approved_at_utc": null,
But when I try to access the second nested object (either 'after', 'before', or 'children' (and since children is an array, I tried to access the first element) I receive the following error:
[17:00:39] TypeError: TypeError: TypeError:
undefined is not an object (evaluating 'myData.data.after')
Which confuses me because I tried to catch the undefined object with my 'if' statement. Any tips, not sure where to go from here. Thank you
javascript json react-native networking javascript-objects
javascript json react-native networking javascript-objects
asked Nov 24 '18 at 1:18
holdenmaudlinholdenmaudlin
11
11
before you call this.setState do a console.log(data) and post here a image of this please.
– Helmer Barcos
Nov 24 '18 at 2:06
Hi Helmer, thanks for responding. Here is the console.log before the setState. (Apologies, I don't know how to format comments): Before Set State: Object { [18:13:18] "data": Object { [18:13:18] "after": "t3_9ysab7", [18:13:18] "before": null, [18:13:18] "children": Array [ [18:13:18] Object { [18:13:18] "data": Object { [18:13:18] "approved_at_utc": null, [18:13:18] "approved_by": null, [18:13:18] "archived": true,
– holdenmaudlin
Nov 24 '18 at 2:14
but i mean a image...
– Helmer Barcos
Nov 24 '18 at 2:37
Here is the link to the console log. Sorry about that.
– holdenmaudlin
Nov 24 '18 at 7:17
add a comment |
before you call this.setState do a console.log(data) and post here a image of this please.
– Helmer Barcos
Nov 24 '18 at 2:06
Hi Helmer, thanks for responding. Here is the console.log before the setState. (Apologies, I don't know how to format comments): Before Set State: Object { [18:13:18] "data": Object { [18:13:18] "after": "t3_9ysab7", [18:13:18] "before": null, [18:13:18] "children": Array [ [18:13:18] Object { [18:13:18] "data": Object { [18:13:18] "approved_at_utc": null, [18:13:18] "approved_by": null, [18:13:18] "archived": true,
– holdenmaudlin
Nov 24 '18 at 2:14
but i mean a image...
– Helmer Barcos
Nov 24 '18 at 2:37
Here is the link to the console log. Sorry about that.
– holdenmaudlin
Nov 24 '18 at 7:17
before you call this.setState do a console.log(data) and post here a image of this please.
– Helmer Barcos
Nov 24 '18 at 2:06
before you call this.setState do a console.log(data) and post here a image of this please.
– Helmer Barcos
Nov 24 '18 at 2:06
Hi Helmer, thanks for responding. Here is the console.log before the setState. (Apologies, I don't know how to format comments): Before Set State: Object { [18:13:18] "data": Object { [18:13:18] "after": "t3_9ysab7", [18:13:18] "before": null, [18:13:18] "children": Array [ [18:13:18] Object { [18:13:18] "data": Object { [18:13:18] "approved_at_utc": null, [18:13:18] "approved_by": null, [18:13:18] "archived": true,
– holdenmaudlin
Nov 24 '18 at 2:14
Hi Helmer, thanks for responding. Here is the console.log before the setState. (Apologies, I don't know how to format comments): Before Set State: Object { [18:13:18] "data": Object { [18:13:18] "after": "t3_9ysab7", [18:13:18] "before": null, [18:13:18] "children": Array [ [18:13:18] Object { [18:13:18] "data": Object { [18:13:18] "approved_at_utc": null, [18:13:18] "approved_by": null, [18:13:18] "archived": true,
– holdenmaudlin
Nov 24 '18 at 2:14
but i mean a image...
– Helmer Barcos
Nov 24 '18 at 2:37
but i mean a image...
– Helmer Barcos
Nov 24 '18 at 2:37
Here is the link to the console log. Sorry about that.
– holdenmaudlin
Nov 24 '18 at 7:17
Here is the link to the console log. Sorry about that.
– holdenmaudlin
Nov 24 '18 at 7:17
add a comment |
1 Answer
1
active
oldest
votes
I could be mistaken here, but in your third line it appears you may not be returning the res.json()
to the next .then()
in your Promise chain, like so:
componentDidMount(){
fetch ('https://www.reddit.com/r/financialindependence.json')
.then(res => {
let resultToPass = res.json();
return resultToPass;
})
.then(data => {
this.setState({
data: data
});
})
.catch( error => {
console.error(error)
})
}
At least superficially that would explain to me why you can access the res
values and log them but they're not making it to your screen components -- they just aren't making it to where you're calling this.setState({data: data})
.
Edit: I broke out the return statement by first assigning a variable, but I believe you could just as easily do return res.json();
and be fine.
Edit 2: Moved the second set of })
for the this.setState()
because I made a formatting goof.
NodeNewb you're a hero! Thank you
– holdenmaudlin
Nov 24 '18 at 7:27
No problem! I'm not sure that's a 100% solution, but I tossed a question out there myself earlier today and figured I'd go for some positive karma and try to help someone else while I await some feedback.
– NodeNewb
Nov 24 '18 at 7:50
I spent a solid day troubleshooting something similar recently, so I'm extra happy to help someone else avoid that peril!
– NodeNewb
Nov 24 '18 at 7:52
I spent yesterday on this! I'll pay it forward when I can
– holdenmaudlin
Nov 24 '18 at 8:20
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53454374%2freact-native-error-accessing-fetched-json-data-objects%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I could be mistaken here, but in your third line it appears you may not be returning the res.json()
to the next .then()
in your Promise chain, like so:
componentDidMount(){
fetch ('https://www.reddit.com/r/financialindependence.json')
.then(res => {
let resultToPass = res.json();
return resultToPass;
})
.then(data => {
this.setState({
data: data
});
})
.catch( error => {
console.error(error)
})
}
At least superficially that would explain to me why you can access the res
values and log them but they're not making it to your screen components -- they just aren't making it to where you're calling this.setState({data: data})
.
Edit: I broke out the return statement by first assigning a variable, but I believe you could just as easily do return res.json();
and be fine.
Edit 2: Moved the second set of })
for the this.setState()
because I made a formatting goof.
NodeNewb you're a hero! Thank you
– holdenmaudlin
Nov 24 '18 at 7:27
No problem! I'm not sure that's a 100% solution, but I tossed a question out there myself earlier today and figured I'd go for some positive karma and try to help someone else while I await some feedback.
– NodeNewb
Nov 24 '18 at 7:50
I spent a solid day troubleshooting something similar recently, so I'm extra happy to help someone else avoid that peril!
– NodeNewb
Nov 24 '18 at 7:52
I spent yesterday on this! I'll pay it forward when I can
– holdenmaudlin
Nov 24 '18 at 8:20
add a comment |
I could be mistaken here, but in your third line it appears you may not be returning the res.json()
to the next .then()
in your Promise chain, like so:
componentDidMount(){
fetch ('https://www.reddit.com/r/financialindependence.json')
.then(res => {
let resultToPass = res.json();
return resultToPass;
})
.then(data => {
this.setState({
data: data
});
})
.catch( error => {
console.error(error)
})
}
At least superficially that would explain to me why you can access the res
values and log them but they're not making it to your screen components -- they just aren't making it to where you're calling this.setState({data: data})
.
Edit: I broke out the return statement by first assigning a variable, but I believe you could just as easily do return res.json();
and be fine.
Edit 2: Moved the second set of })
for the this.setState()
because I made a formatting goof.
NodeNewb you're a hero! Thank you
– holdenmaudlin
Nov 24 '18 at 7:27
No problem! I'm not sure that's a 100% solution, but I tossed a question out there myself earlier today and figured I'd go for some positive karma and try to help someone else while I await some feedback.
– NodeNewb
Nov 24 '18 at 7:50
I spent a solid day troubleshooting something similar recently, so I'm extra happy to help someone else avoid that peril!
– NodeNewb
Nov 24 '18 at 7:52
I spent yesterday on this! I'll pay it forward when I can
– holdenmaudlin
Nov 24 '18 at 8:20
add a comment |
I could be mistaken here, but in your third line it appears you may not be returning the res.json()
to the next .then()
in your Promise chain, like so:
componentDidMount(){
fetch ('https://www.reddit.com/r/financialindependence.json')
.then(res => {
let resultToPass = res.json();
return resultToPass;
})
.then(data => {
this.setState({
data: data
});
})
.catch( error => {
console.error(error)
})
}
At least superficially that would explain to me why you can access the res
values and log them but they're not making it to your screen components -- they just aren't making it to where you're calling this.setState({data: data})
.
Edit: I broke out the return statement by first assigning a variable, but I believe you could just as easily do return res.json();
and be fine.
Edit 2: Moved the second set of })
for the this.setState()
because I made a formatting goof.
I could be mistaken here, but in your third line it appears you may not be returning the res.json()
to the next .then()
in your Promise chain, like so:
componentDidMount(){
fetch ('https://www.reddit.com/r/financialindependence.json')
.then(res => {
let resultToPass = res.json();
return resultToPass;
})
.then(data => {
this.setState({
data: data
});
})
.catch( error => {
console.error(error)
})
}
At least superficially that would explain to me why you can access the res
values and log them but they're not making it to your screen components -- they just aren't making it to where you're calling this.setState({data: data})
.
Edit: I broke out the return statement by first assigning a variable, but I believe you could just as easily do return res.json();
and be fine.
Edit 2: Moved the second set of })
for the this.setState()
because I made a formatting goof.
edited Nov 24 '18 at 6:33
answered Nov 24 '18 at 6:24
NodeNewbNodeNewb
1315
1315
NodeNewb you're a hero! Thank you
– holdenmaudlin
Nov 24 '18 at 7:27
No problem! I'm not sure that's a 100% solution, but I tossed a question out there myself earlier today and figured I'd go for some positive karma and try to help someone else while I await some feedback.
– NodeNewb
Nov 24 '18 at 7:50
I spent a solid day troubleshooting something similar recently, so I'm extra happy to help someone else avoid that peril!
– NodeNewb
Nov 24 '18 at 7:52
I spent yesterday on this! I'll pay it forward when I can
– holdenmaudlin
Nov 24 '18 at 8:20
add a comment |
NodeNewb you're a hero! Thank you
– holdenmaudlin
Nov 24 '18 at 7:27
No problem! I'm not sure that's a 100% solution, but I tossed a question out there myself earlier today and figured I'd go for some positive karma and try to help someone else while I await some feedback.
– NodeNewb
Nov 24 '18 at 7:50
I spent a solid day troubleshooting something similar recently, so I'm extra happy to help someone else avoid that peril!
– NodeNewb
Nov 24 '18 at 7:52
I spent yesterday on this! I'll pay it forward when I can
– holdenmaudlin
Nov 24 '18 at 8:20
NodeNewb you're a hero! Thank you
– holdenmaudlin
Nov 24 '18 at 7:27
NodeNewb you're a hero! Thank you
– holdenmaudlin
Nov 24 '18 at 7:27
No problem! I'm not sure that's a 100% solution, but I tossed a question out there myself earlier today and figured I'd go for some positive karma and try to help someone else while I await some feedback.
– NodeNewb
Nov 24 '18 at 7:50
No problem! I'm not sure that's a 100% solution, but I tossed a question out there myself earlier today and figured I'd go for some positive karma and try to help someone else while I await some feedback.
– NodeNewb
Nov 24 '18 at 7:50
I spent a solid day troubleshooting something similar recently, so I'm extra happy to help someone else avoid that peril!
– NodeNewb
Nov 24 '18 at 7:52
I spent a solid day troubleshooting something similar recently, so I'm extra happy to help someone else avoid that peril!
– NodeNewb
Nov 24 '18 at 7:52
I spent yesterday on this! I'll pay it forward when I can
– holdenmaudlin
Nov 24 '18 at 8:20
I spent yesterday on this! I'll pay it forward when I can
– holdenmaudlin
Nov 24 '18 at 8:20
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53454374%2freact-native-error-accessing-fetched-json-data-objects%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
before you call this.setState do a console.log(data) and post here a image of this please.
– Helmer Barcos
Nov 24 '18 at 2:06
Hi Helmer, thanks for responding. Here is the console.log before the setState. (Apologies, I don't know how to format comments): Before Set State: Object { [18:13:18] "data": Object { [18:13:18] "after": "t3_9ysab7", [18:13:18] "before": null, [18:13:18] "children": Array [ [18:13:18] Object { [18:13:18] "data": Object { [18:13:18] "approved_at_utc": null, [18:13:18] "approved_by": null, [18:13:18] "archived": true,
– holdenmaudlin
Nov 24 '18 at 2:14
but i mean a image...
– Helmer Barcos
Nov 24 '18 at 2:37
Here is the link to the console log. Sorry about that.
– holdenmaudlin
Nov 24 '18 at 7:17