How to correctly handle data object from REST API using fetch in React
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have a React app that receives an object from WordPress site using fetch() over rest api (v2/posts).
The object contains an "acf" entry which holds ~30 sub entries: some arrays and some are objects. All generated with Advanced Custom Field plugin.
The application is updating this data (using this.state) and the server (fetch/post) upon user request. All is working. I am not using express, redux and the like.
My current problem is that I cannot access the internals of the "acf" entry in render() function, while I can access it within the fetch response.
I would like to be able to acccess
cpt['acf']['entry']['subentry']
using the entry's names. But I get "cannot access property of undefined..."
Example:
// In constructor:
// this.state = {
// isLoading: false, title:"", terms:, cpt:
// }
...
fetch(url)
.then(response => {
if (response.ok)
return response.json()
throw new Error('fetchTerm: Something went wrong ...')
})
.then(cpt => {
var terms = Object.entries(cpt['acf'])
var title = Object.values(cpt['title'])
this.setState({cpt, terms, title, isLoading: false})
.then( () => {
console.log("title",this.state.cpt['title'])
console.log("FAMILYLOG",this.state.cpt['acf']['familylog_array']) // shows ok
})
return (true) // void - no one is looking...
})
//...
render(){
if (this.state.isLoading)
return (Loading)
// occurs only after fetch resturn and this.state variables are instanciated
console.log("title", this.state.title) // shows OK
console.log("FAIL", this.state.cpt['title']) // cannot access property of NULL/Undefined...
console.log("FAMILYLOG",this.state.cpt['acf']['familylog_array']) // same problem
}
What am I missing?
I would like to be able to acccess cpt['acf']['entry']['subentry']
Should I regenerate the cpt into arrays/objects in the Component state?
reactjs rest fetch advanced-custom-fields
add a comment |
I have a React app that receives an object from WordPress site using fetch() over rest api (v2/posts).
The object contains an "acf" entry which holds ~30 sub entries: some arrays and some are objects. All generated with Advanced Custom Field plugin.
The application is updating this data (using this.state) and the server (fetch/post) upon user request. All is working. I am not using express, redux and the like.
My current problem is that I cannot access the internals of the "acf" entry in render() function, while I can access it within the fetch response.
I would like to be able to acccess
cpt['acf']['entry']['subentry']
using the entry's names. But I get "cannot access property of undefined..."
Example:
// In constructor:
// this.state = {
// isLoading: false, title:"", terms:, cpt:
// }
...
fetch(url)
.then(response => {
if (response.ok)
return response.json()
throw new Error('fetchTerm: Something went wrong ...')
})
.then(cpt => {
var terms = Object.entries(cpt['acf'])
var title = Object.values(cpt['title'])
this.setState({cpt, terms, title, isLoading: false})
.then( () => {
console.log("title",this.state.cpt['title'])
console.log("FAMILYLOG",this.state.cpt['acf']['familylog_array']) // shows ok
})
return (true) // void - no one is looking...
})
//...
render(){
if (this.state.isLoading)
return (Loading)
// occurs only after fetch resturn and this.state variables are instanciated
console.log("title", this.state.title) // shows OK
console.log("FAIL", this.state.cpt['title']) // cannot access property of NULL/Undefined...
console.log("FAMILYLOG",this.state.cpt['acf']['familylog_array']) // same problem
}
What am I missing?
I would like to be able to acccess cpt['acf']['entry']['subentry']
Should I regenerate the cpt into arrays/objects in the Component state?
reactjs rest fetch advanced-custom-fields
Can you update your post to include a console.log of the cpt variable after you put it in state?
– Shawn Andrews
Nov 27 '18 at 2:29
Just added the console.log() in the fetch() call and in render.
– Mulli
Nov 27 '18 at 4:16
add a comment |
I have a React app that receives an object from WordPress site using fetch() over rest api (v2/posts).
The object contains an "acf" entry which holds ~30 sub entries: some arrays and some are objects. All generated with Advanced Custom Field plugin.
The application is updating this data (using this.state) and the server (fetch/post) upon user request. All is working. I am not using express, redux and the like.
My current problem is that I cannot access the internals of the "acf" entry in render() function, while I can access it within the fetch response.
I would like to be able to acccess
cpt['acf']['entry']['subentry']
using the entry's names. But I get "cannot access property of undefined..."
Example:
// In constructor:
// this.state = {
// isLoading: false, title:"", terms:, cpt:
// }
...
fetch(url)
.then(response => {
if (response.ok)
return response.json()
throw new Error('fetchTerm: Something went wrong ...')
})
.then(cpt => {
var terms = Object.entries(cpt['acf'])
var title = Object.values(cpt['title'])
this.setState({cpt, terms, title, isLoading: false})
.then( () => {
console.log("title",this.state.cpt['title'])
console.log("FAMILYLOG",this.state.cpt['acf']['familylog_array']) // shows ok
})
return (true) // void - no one is looking...
})
//...
render(){
if (this.state.isLoading)
return (Loading)
// occurs only after fetch resturn and this.state variables are instanciated
console.log("title", this.state.title) // shows OK
console.log("FAIL", this.state.cpt['title']) // cannot access property of NULL/Undefined...
console.log("FAMILYLOG",this.state.cpt['acf']['familylog_array']) // same problem
}
What am I missing?
I would like to be able to acccess cpt['acf']['entry']['subentry']
Should I regenerate the cpt into arrays/objects in the Component state?
reactjs rest fetch advanced-custom-fields
I have a React app that receives an object from WordPress site using fetch() over rest api (v2/posts).
The object contains an "acf" entry which holds ~30 sub entries: some arrays and some are objects. All generated with Advanced Custom Field plugin.
The application is updating this data (using this.state) and the server (fetch/post) upon user request. All is working. I am not using express, redux and the like.
My current problem is that I cannot access the internals of the "acf" entry in render() function, while I can access it within the fetch response.
I would like to be able to acccess
cpt['acf']['entry']['subentry']
using the entry's names. But I get "cannot access property of undefined..."
Example:
// In constructor:
// this.state = {
// isLoading: false, title:"", terms:, cpt:
// }
...
fetch(url)
.then(response => {
if (response.ok)
return response.json()
throw new Error('fetchTerm: Something went wrong ...')
})
.then(cpt => {
var terms = Object.entries(cpt['acf'])
var title = Object.values(cpt['title'])
this.setState({cpt, terms, title, isLoading: false})
.then( () => {
console.log("title",this.state.cpt['title'])
console.log("FAMILYLOG",this.state.cpt['acf']['familylog_array']) // shows ok
})
return (true) // void - no one is looking...
})
//...
render(){
if (this.state.isLoading)
return (Loading)
// occurs only after fetch resturn and this.state variables are instanciated
console.log("title", this.state.title) // shows OK
console.log("FAIL", this.state.cpt['title']) // cannot access property of NULL/Undefined...
console.log("FAMILYLOG",this.state.cpt['acf']['familylog_array']) // same problem
}
What am I missing?
I would like to be able to acccess cpt['acf']['entry']['subentry']
Should I regenerate the cpt into arrays/objects in the Component state?
reactjs rest fetch advanced-custom-fields
reactjs rest fetch advanced-custom-fields
edited Nov 27 '18 at 3:53
Mulli
asked Nov 27 '18 at 1:56
MulliMulli
361214
361214
Can you update your post to include a console.log of the cpt variable after you put it in state?
– Shawn Andrews
Nov 27 '18 at 2:29
Just added the console.log() in the fetch() call and in render.
– Mulli
Nov 27 '18 at 4:16
add a comment |
Can you update your post to include a console.log of the cpt variable after you put it in state?
– Shawn Andrews
Nov 27 '18 at 2:29
Just added the console.log() in the fetch() call and in render.
– Mulli
Nov 27 '18 at 4:16
Can you update your post to include a console.log of the cpt variable after you put it in state?
– Shawn Andrews
Nov 27 '18 at 2:29
Can you update your post to include a console.log of the cpt variable after you put it in state?
– Shawn Andrews
Nov 27 '18 at 2:29
Just added the console.log() in the fetch() call and in render.
– Mulli
Nov 27 '18 at 4:16
Just added the console.log() in the fetch() call and in render.
– Mulli
Nov 27 '18 at 4:16
add a comment |
1 Answer
1
active
oldest
votes
according to your code you initialize
cpt:
(which should rather be an object though) in the constructor and then you make a call to the api and only when the promise is resolved you will modify the state, the thing is it happens after the first render call is done, so you get "cannot access property of NULL/Undefined" for cpt['acf']['entry']['subentry']
, though this.state.cpt['title']
will rather return undefined
in the render()
Upd: To prevent this, isLoading should have been set as true in the constructor
Please note the isLoading flag that prevents access to this.state prior to fetch() completion. As for the cpt definition. I did try to set it to an object and got same results
– Mulli
Nov 27 '18 at 3:51
you might want to set isLoading as true when you assign it for the first time
– Alex Tepliashin
Nov 27 '18 at 4:10
You are right! thanks. I set it to true in componentDidMount. Just realized its better in the constructor. thanks
– Mulli
Nov 27 '18 at 4:22
Great, glad to help!
– Alex Tepliashin
Nov 27 '18 at 4:25
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%2f53491674%2fhow-to-correctly-handle-data-object-from-rest-api-using-fetch-in-react%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
according to your code you initialize
cpt:
(which should rather be an object though) in the constructor and then you make a call to the api and only when the promise is resolved you will modify the state, the thing is it happens after the first render call is done, so you get "cannot access property of NULL/Undefined" for cpt['acf']['entry']['subentry']
, though this.state.cpt['title']
will rather return undefined
in the render()
Upd: To prevent this, isLoading should have been set as true in the constructor
Please note the isLoading flag that prevents access to this.state prior to fetch() completion. As for the cpt definition. I did try to set it to an object and got same results
– Mulli
Nov 27 '18 at 3:51
you might want to set isLoading as true when you assign it for the first time
– Alex Tepliashin
Nov 27 '18 at 4:10
You are right! thanks. I set it to true in componentDidMount. Just realized its better in the constructor. thanks
– Mulli
Nov 27 '18 at 4:22
Great, glad to help!
– Alex Tepliashin
Nov 27 '18 at 4:25
add a comment |
according to your code you initialize
cpt:
(which should rather be an object though) in the constructor and then you make a call to the api and only when the promise is resolved you will modify the state, the thing is it happens after the first render call is done, so you get "cannot access property of NULL/Undefined" for cpt['acf']['entry']['subentry']
, though this.state.cpt['title']
will rather return undefined
in the render()
Upd: To prevent this, isLoading should have been set as true in the constructor
Please note the isLoading flag that prevents access to this.state prior to fetch() completion. As for the cpt definition. I did try to set it to an object and got same results
– Mulli
Nov 27 '18 at 3:51
you might want to set isLoading as true when you assign it for the first time
– Alex Tepliashin
Nov 27 '18 at 4:10
You are right! thanks. I set it to true in componentDidMount. Just realized its better in the constructor. thanks
– Mulli
Nov 27 '18 at 4:22
Great, glad to help!
– Alex Tepliashin
Nov 27 '18 at 4:25
add a comment |
according to your code you initialize
cpt:
(which should rather be an object though) in the constructor and then you make a call to the api and only when the promise is resolved you will modify the state, the thing is it happens after the first render call is done, so you get "cannot access property of NULL/Undefined" for cpt['acf']['entry']['subentry']
, though this.state.cpt['title']
will rather return undefined
in the render()
Upd: To prevent this, isLoading should have been set as true in the constructor
according to your code you initialize
cpt:
(which should rather be an object though) in the constructor and then you make a call to the api and only when the promise is resolved you will modify the state, the thing is it happens after the first render call is done, so you get "cannot access property of NULL/Undefined" for cpt['acf']['entry']['subentry']
, though this.state.cpt['title']
will rather return undefined
in the render()
Upd: To prevent this, isLoading should have been set as true in the constructor
edited Nov 27 '18 at 4:25
answered Nov 27 '18 at 2:29
Alex TepliashinAlex Tepliashin
613
613
Please note the isLoading flag that prevents access to this.state prior to fetch() completion. As for the cpt definition. I did try to set it to an object and got same results
– Mulli
Nov 27 '18 at 3:51
you might want to set isLoading as true when you assign it for the first time
– Alex Tepliashin
Nov 27 '18 at 4:10
You are right! thanks. I set it to true in componentDidMount. Just realized its better in the constructor. thanks
– Mulli
Nov 27 '18 at 4:22
Great, glad to help!
– Alex Tepliashin
Nov 27 '18 at 4:25
add a comment |
Please note the isLoading flag that prevents access to this.state prior to fetch() completion. As for the cpt definition. I did try to set it to an object and got same results
– Mulli
Nov 27 '18 at 3:51
you might want to set isLoading as true when you assign it for the first time
– Alex Tepliashin
Nov 27 '18 at 4:10
You are right! thanks. I set it to true in componentDidMount. Just realized its better in the constructor. thanks
– Mulli
Nov 27 '18 at 4:22
Great, glad to help!
– Alex Tepliashin
Nov 27 '18 at 4:25
Please note the isLoading flag that prevents access to this.state prior to fetch() completion. As for the cpt definition. I did try to set it to an object and got same results
– Mulli
Nov 27 '18 at 3:51
Please note the isLoading flag that prevents access to this.state prior to fetch() completion. As for the cpt definition. I did try to set it to an object and got same results
– Mulli
Nov 27 '18 at 3:51
you might want to set isLoading as true when you assign it for the first time
– Alex Tepliashin
Nov 27 '18 at 4:10
you might want to set isLoading as true when you assign it for the first time
– Alex Tepliashin
Nov 27 '18 at 4:10
You are right! thanks. I set it to true in componentDidMount. Just realized its better in the constructor. thanks
– Mulli
Nov 27 '18 at 4:22
You are right! thanks. I set it to true in componentDidMount. Just realized its better in the constructor. thanks
– Mulli
Nov 27 '18 at 4:22
Great, glad to help!
– Alex Tepliashin
Nov 27 '18 at 4:25
Great, glad to help!
– Alex Tepliashin
Nov 27 '18 at 4:25
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%2f53491674%2fhow-to-correctly-handle-data-object-from-rest-api-using-fetch-in-react%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
Can you update your post to include a console.log of the cpt variable after you put it in state?
– Shawn Andrews
Nov 27 '18 at 2:29
Just added the console.log() in the fetch() call and in render.
– Mulli
Nov 27 '18 at 4:16