How to fetch deeply nested data from the response body into state





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I've created a show page in react that is supposed to display the name of the selected student, the instrument(s) they play, and their assignments.



The database (simplified): user(student) data >many-to-many-relationship< instrument >many-to-many-relationship< assignment



While I can pull a single user(student) object into state and display that student's name, accessing the associated instrument(s) and assignment(s) has been stumping me.



Here's what the body.user looks like when I log it to the console:



{user: {…}}
user:
assignments: Array(1)
0: {id: 6, name: "Concert Cb Scale", description: "Record this
scale to Ensemble at the start of the …nscribe the scale if
your instrument requires it!", created_at: "2018-11-
24T22:16:51.460Z", updated_at: "2018-11-24T22:16:51.460Z"}
length: 1
__proto__: Array(0)
first_name: "June"
id: 10
instrument_sections: Array(1)
0: {id: 7, instrument: "french horn", created_at: "2018-11-
24T22:16:51.356Z", updated_at: "2018-11-24T22:16:51.356Z"}
length: 1
__proto__: Array(0)
last_name: "Cenadaigh"


I can't seem to get at body.user.instrument_sections (and thenceforth to instrument:). I haven't attempted getting at body.user.assignments since it looks to me like I'll get the same errors.



The error in question is, from the console: "Error in fetch: Objects are not valid as a React child (found: object with keys {id, instrument, created_at, updated_at}). If you meant to render a collection of children, use an array instead or wrap the object using createFragment(object) from the React add-ons. Check the render method of StudentShow." I understand that it's having an issue that I'm pointing at an object within the user object, but I don't understand how to go about the suggestions the error makes. Furthermore, if I try to change this.setState({ . . . instrumentSections: body.user.instrument_sections to include .instrument, the console comes back with simply undefined.



My further attempts to get at instrument have come from:




  1. Trying to figure out what should go between body.user.instrument_sections and .instrument (if anything).


  2. How to map through body.user or the student state to get at the instrument and put it into state. (Eventually, I'll have seed data that will have students with multiple instruments instead of just one, so I have that state held in an array.)



And finally, here's the code I currently have:



class StudentShow extends Component {
constructor(props){
super(props)
this.state = {
student: {},
instrumentSections: ,
assignments:
}
}
componentDidMount(){
let studentId = this.props.params.id
console.log(`${this.props.params.id}`)
fetch(`/api/v1/users/${studentId}`)
.then(response => {
if (response.ok) {
return response;
} else {
let errorMessage = `${response.status}
(${response.statusText})`;
error = new Error(errorMessage);
throw(error);
}
})
.then(response => response.json())
.then(body => {
console.log(body);
this.setState({ student: body.user, instrumentSections:
body.user.instrument_sections })
console.log(this.state.student);
console.log(this.state.instrumentSections);
})
.catch(error => console.error(`Error in fetch: ${error.message}`));
}
render(){
return (
<div className="text-center student-show">
<h1>{this.state.student.first_name}
{this.state.student.last_name}</h1>
<h2>Your section(s): </h2>
<h4></h4>
<h2>This week's assignment(s): </h2>
<h4></h4>
</div>
)
}
}









share|improve this question

























  • If I understand your question correctly, you want to get access to this.state.student. instrument_sections where instrument_sections looks something like this: ["french horn", "tuba", "trombone"] Is that correct?

    – Maninacan
    Jan 4 at 19:44











  • That will become the goal once I can get instruments (like "french horn", "tuba", "trombone") into state, which is the current goal and I'm finding tricky since the instruments with which I want to build the array are deeply nested in the fetch body. I'm about to try amirify's suggestion. Fingers crossed.

    – Adam J
    Jan 8 at 16:03


















0















I've created a show page in react that is supposed to display the name of the selected student, the instrument(s) they play, and their assignments.



The database (simplified): user(student) data >many-to-many-relationship< instrument >many-to-many-relationship< assignment



While I can pull a single user(student) object into state and display that student's name, accessing the associated instrument(s) and assignment(s) has been stumping me.



Here's what the body.user looks like when I log it to the console:



{user: {…}}
user:
assignments: Array(1)
0: {id: 6, name: "Concert Cb Scale", description: "Record this
scale to Ensemble at the start of the …nscribe the scale if
your instrument requires it!", created_at: "2018-11-
24T22:16:51.460Z", updated_at: "2018-11-24T22:16:51.460Z"}
length: 1
__proto__: Array(0)
first_name: "June"
id: 10
instrument_sections: Array(1)
0: {id: 7, instrument: "french horn", created_at: "2018-11-
24T22:16:51.356Z", updated_at: "2018-11-24T22:16:51.356Z"}
length: 1
__proto__: Array(0)
last_name: "Cenadaigh"


I can't seem to get at body.user.instrument_sections (and thenceforth to instrument:). I haven't attempted getting at body.user.assignments since it looks to me like I'll get the same errors.



The error in question is, from the console: "Error in fetch: Objects are not valid as a React child (found: object with keys {id, instrument, created_at, updated_at}). If you meant to render a collection of children, use an array instead or wrap the object using createFragment(object) from the React add-ons. Check the render method of StudentShow." I understand that it's having an issue that I'm pointing at an object within the user object, but I don't understand how to go about the suggestions the error makes. Furthermore, if I try to change this.setState({ . . . instrumentSections: body.user.instrument_sections to include .instrument, the console comes back with simply undefined.



My further attempts to get at instrument have come from:




  1. Trying to figure out what should go between body.user.instrument_sections and .instrument (if anything).


  2. How to map through body.user or the student state to get at the instrument and put it into state. (Eventually, I'll have seed data that will have students with multiple instruments instead of just one, so I have that state held in an array.)



And finally, here's the code I currently have:



class StudentShow extends Component {
constructor(props){
super(props)
this.state = {
student: {},
instrumentSections: ,
assignments:
}
}
componentDidMount(){
let studentId = this.props.params.id
console.log(`${this.props.params.id}`)
fetch(`/api/v1/users/${studentId}`)
.then(response => {
if (response.ok) {
return response;
} else {
let errorMessage = `${response.status}
(${response.statusText})`;
error = new Error(errorMessage);
throw(error);
}
})
.then(response => response.json())
.then(body => {
console.log(body);
this.setState({ student: body.user, instrumentSections:
body.user.instrument_sections })
console.log(this.state.student);
console.log(this.state.instrumentSections);
})
.catch(error => console.error(`Error in fetch: ${error.message}`));
}
render(){
return (
<div className="text-center student-show">
<h1>{this.state.student.first_name}
{this.state.student.last_name}</h1>
<h2>Your section(s): </h2>
<h4></h4>
<h2>This week's assignment(s): </h2>
<h4></h4>
</div>
)
}
}









share|improve this question

























  • If I understand your question correctly, you want to get access to this.state.student. instrument_sections where instrument_sections looks something like this: ["french horn", "tuba", "trombone"] Is that correct?

    – Maninacan
    Jan 4 at 19:44











  • That will become the goal once I can get instruments (like "french horn", "tuba", "trombone") into state, which is the current goal and I'm finding tricky since the instruments with which I want to build the array are deeply nested in the fetch body. I'm about to try amirify's suggestion. Fingers crossed.

    – Adam J
    Jan 8 at 16:03














0












0








0








I've created a show page in react that is supposed to display the name of the selected student, the instrument(s) they play, and their assignments.



The database (simplified): user(student) data >many-to-many-relationship< instrument >many-to-many-relationship< assignment



While I can pull a single user(student) object into state and display that student's name, accessing the associated instrument(s) and assignment(s) has been stumping me.



Here's what the body.user looks like when I log it to the console:



{user: {…}}
user:
assignments: Array(1)
0: {id: 6, name: "Concert Cb Scale", description: "Record this
scale to Ensemble at the start of the …nscribe the scale if
your instrument requires it!", created_at: "2018-11-
24T22:16:51.460Z", updated_at: "2018-11-24T22:16:51.460Z"}
length: 1
__proto__: Array(0)
first_name: "June"
id: 10
instrument_sections: Array(1)
0: {id: 7, instrument: "french horn", created_at: "2018-11-
24T22:16:51.356Z", updated_at: "2018-11-24T22:16:51.356Z"}
length: 1
__proto__: Array(0)
last_name: "Cenadaigh"


I can't seem to get at body.user.instrument_sections (and thenceforth to instrument:). I haven't attempted getting at body.user.assignments since it looks to me like I'll get the same errors.



The error in question is, from the console: "Error in fetch: Objects are not valid as a React child (found: object with keys {id, instrument, created_at, updated_at}). If you meant to render a collection of children, use an array instead or wrap the object using createFragment(object) from the React add-ons. Check the render method of StudentShow." I understand that it's having an issue that I'm pointing at an object within the user object, but I don't understand how to go about the suggestions the error makes. Furthermore, if I try to change this.setState({ . . . instrumentSections: body.user.instrument_sections to include .instrument, the console comes back with simply undefined.



My further attempts to get at instrument have come from:




  1. Trying to figure out what should go between body.user.instrument_sections and .instrument (if anything).


  2. How to map through body.user or the student state to get at the instrument and put it into state. (Eventually, I'll have seed data that will have students with multiple instruments instead of just one, so I have that state held in an array.)



And finally, here's the code I currently have:



class StudentShow extends Component {
constructor(props){
super(props)
this.state = {
student: {},
instrumentSections: ,
assignments:
}
}
componentDidMount(){
let studentId = this.props.params.id
console.log(`${this.props.params.id}`)
fetch(`/api/v1/users/${studentId}`)
.then(response => {
if (response.ok) {
return response;
} else {
let errorMessage = `${response.status}
(${response.statusText})`;
error = new Error(errorMessage);
throw(error);
}
})
.then(response => response.json())
.then(body => {
console.log(body);
this.setState({ student: body.user, instrumentSections:
body.user.instrument_sections })
console.log(this.state.student);
console.log(this.state.instrumentSections);
})
.catch(error => console.error(`Error in fetch: ${error.message}`));
}
render(){
return (
<div className="text-center student-show">
<h1>{this.state.student.first_name}
{this.state.student.last_name}</h1>
<h2>Your section(s): </h2>
<h4></h4>
<h2>This week's assignment(s): </h2>
<h4></h4>
</div>
)
}
}









share|improve this question
















I've created a show page in react that is supposed to display the name of the selected student, the instrument(s) they play, and their assignments.



The database (simplified): user(student) data >many-to-many-relationship< instrument >many-to-many-relationship< assignment



While I can pull a single user(student) object into state and display that student's name, accessing the associated instrument(s) and assignment(s) has been stumping me.



Here's what the body.user looks like when I log it to the console:



{user: {…}}
user:
assignments: Array(1)
0: {id: 6, name: "Concert Cb Scale", description: "Record this
scale to Ensemble at the start of the …nscribe the scale if
your instrument requires it!", created_at: "2018-11-
24T22:16:51.460Z", updated_at: "2018-11-24T22:16:51.460Z"}
length: 1
__proto__: Array(0)
first_name: "June"
id: 10
instrument_sections: Array(1)
0: {id: 7, instrument: "french horn", created_at: "2018-11-
24T22:16:51.356Z", updated_at: "2018-11-24T22:16:51.356Z"}
length: 1
__proto__: Array(0)
last_name: "Cenadaigh"


I can't seem to get at body.user.instrument_sections (and thenceforth to instrument:). I haven't attempted getting at body.user.assignments since it looks to me like I'll get the same errors.



The error in question is, from the console: "Error in fetch: Objects are not valid as a React child (found: object with keys {id, instrument, created_at, updated_at}). If you meant to render a collection of children, use an array instead or wrap the object using createFragment(object) from the React add-ons. Check the render method of StudentShow." I understand that it's having an issue that I'm pointing at an object within the user object, but I don't understand how to go about the suggestions the error makes. Furthermore, if I try to change this.setState({ . . . instrumentSections: body.user.instrument_sections to include .instrument, the console comes back with simply undefined.



My further attempts to get at instrument have come from:




  1. Trying to figure out what should go between body.user.instrument_sections and .instrument (if anything).


  2. How to map through body.user or the student state to get at the instrument and put it into state. (Eventually, I'll have seed data that will have students with multiple instruments instead of just one, so I have that state held in an array.)



And finally, here's the code I currently have:



class StudentShow extends Component {
constructor(props){
super(props)
this.state = {
student: {},
instrumentSections: ,
assignments:
}
}
componentDidMount(){
let studentId = this.props.params.id
console.log(`${this.props.params.id}`)
fetch(`/api/v1/users/${studentId}`)
.then(response => {
if (response.ok) {
return response;
} else {
let errorMessage = `${response.status}
(${response.statusText})`;
error = new Error(errorMessage);
throw(error);
}
})
.then(response => response.json())
.then(body => {
console.log(body);
this.setState({ student: body.user, instrumentSections:
body.user.instrument_sections })
console.log(this.state.student);
console.log(this.state.instrumentSections);
})
.catch(error => console.error(`Error in fetch: ${error.message}`));
}
render(){
return (
<div className="text-center student-show">
<h1>{this.state.student.first_name}
{this.state.student.last_name}</h1>
<h2>Your section(s): </h2>
<h4></h4>
<h2>This week's assignment(s): </h2>
<h4></h4>
</div>
)
}
}






javascript reactjs fetch stateful






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 4 at 19:33









wlh

1,8901824




1,8901824










asked Jan 4 at 18:24









Adam JAdam J

65




65













  • If I understand your question correctly, you want to get access to this.state.student. instrument_sections where instrument_sections looks something like this: ["french horn", "tuba", "trombone"] Is that correct?

    – Maninacan
    Jan 4 at 19:44











  • That will become the goal once I can get instruments (like "french horn", "tuba", "trombone") into state, which is the current goal and I'm finding tricky since the instruments with which I want to build the array are deeply nested in the fetch body. I'm about to try amirify's suggestion. Fingers crossed.

    – Adam J
    Jan 8 at 16:03



















  • If I understand your question correctly, you want to get access to this.state.student. instrument_sections where instrument_sections looks something like this: ["french horn", "tuba", "trombone"] Is that correct?

    – Maninacan
    Jan 4 at 19:44











  • That will become the goal once I can get instruments (like "french horn", "tuba", "trombone") into state, which is the current goal and I'm finding tricky since the instruments with which I want to build the array are deeply nested in the fetch body. I'm about to try amirify's suggestion. Fingers crossed.

    – Adam J
    Jan 8 at 16:03

















If I understand your question correctly, you want to get access to this.state.student. instrument_sections where instrument_sections looks something like this: ["french horn", "tuba", "trombone"] Is that correct?

– Maninacan
Jan 4 at 19:44





If I understand your question correctly, you want to get access to this.state.student. instrument_sections where instrument_sections looks something like this: ["french horn", "tuba", "trombone"] Is that correct?

– Maninacan
Jan 4 at 19:44













That will become the goal once I can get instruments (like "french horn", "tuba", "trombone") into state, which is the current goal and I'm finding tricky since the instruments with which I want to build the array are deeply nested in the fetch body. I'm about to try amirify's suggestion. Fingers crossed.

– Adam J
Jan 8 at 16:03





That will become the goal once I can get instruments (like "french horn", "tuba", "trombone") into state, which is the current goal and I'm finding tricky since the instruments with which I want to build the array are deeply nested in the fetch body. I'm about to try amirify's suggestion. Fingers crossed.

– Adam J
Jan 8 at 16:03












2 Answers
2






active

oldest

votes


















1














As you can see in your consoleinstrument_sections is an array of objects so to get the first instrument for example you should get it like:
body.user.instrument_sections[0].instrument.

and instead of instrumentSections: body.user.instrument_sections try
instrumentSections: body.user.instrument_sections.slice() or
instrumentSections: Array.from(body.user.instrument_sections)

and let me know if this solves your problem.






share|improve this answer


























  • It doesn't seem to work, but it's still progress, thank goodness! After brushing up on the functions you're suggesting, I totally see where you're going. (updating . . .)

    – Adam J
    Jan 11 at 15:41













  • body.user.instrument_sections[0].instrument works to get the first instrument, which is encouraging. instrumentSections: body.user.instrument_sections.slice() and instrumentSections: Array.from(body.user.instrument_sections) don't work (same error), but I bet they're close: both try putting each instrument_sections object into state whereas I need to look within each instrument_sections to the instrument key and extract its pair. If I could somehow do something like body.user.instrument_sections[all].instrument I'd be in business. Perhaps use some forEach() fx or the like?

    – Adam J
    Jan 11 at 16:11



















-1














Me and the guy I worked with aren't sure if it's the most efficient method or if it's kosher with reactjs style, but at long last, the following javascript loop and accessories will work. After .then(response => response.json()):



.then(body => {
const sections = ;
for(let i = 0; i < body.user.instrument_sections.length; i++){ sections.push(body.user.instrument_sections[i].instrument);
};



And change the setState to this:



this.setState({ student: body.user, instrumentSections: sections)}



So what happens is js reaches into the body and through iteration grabs each relevant instrument, which goes into the sections array, and finally you set the state of instrumentSections equal to sections.






share|improve this answer
























  • -1? Uh oh. Did I break etiquette? Is there something particularly wrong with the solution even if it's functional? Something else?

    – Adam J
    Feb 7 at 19:17












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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54044237%2fhow-to-fetch-deeply-nested-data-from-the-response-body-into-state%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














As you can see in your consoleinstrument_sections is an array of objects so to get the first instrument for example you should get it like:
body.user.instrument_sections[0].instrument.

and instead of instrumentSections: body.user.instrument_sections try
instrumentSections: body.user.instrument_sections.slice() or
instrumentSections: Array.from(body.user.instrument_sections)

and let me know if this solves your problem.






share|improve this answer


























  • It doesn't seem to work, but it's still progress, thank goodness! After brushing up on the functions you're suggesting, I totally see where you're going. (updating . . .)

    – Adam J
    Jan 11 at 15:41













  • body.user.instrument_sections[0].instrument works to get the first instrument, which is encouraging. instrumentSections: body.user.instrument_sections.slice() and instrumentSections: Array.from(body.user.instrument_sections) don't work (same error), but I bet they're close: both try putting each instrument_sections object into state whereas I need to look within each instrument_sections to the instrument key and extract its pair. If I could somehow do something like body.user.instrument_sections[all].instrument I'd be in business. Perhaps use some forEach() fx or the like?

    – Adam J
    Jan 11 at 16:11
















1














As you can see in your consoleinstrument_sections is an array of objects so to get the first instrument for example you should get it like:
body.user.instrument_sections[0].instrument.

and instead of instrumentSections: body.user.instrument_sections try
instrumentSections: body.user.instrument_sections.slice() or
instrumentSections: Array.from(body.user.instrument_sections)

and let me know if this solves your problem.






share|improve this answer


























  • It doesn't seem to work, but it's still progress, thank goodness! After brushing up on the functions you're suggesting, I totally see where you're going. (updating . . .)

    – Adam J
    Jan 11 at 15:41













  • body.user.instrument_sections[0].instrument works to get the first instrument, which is encouraging. instrumentSections: body.user.instrument_sections.slice() and instrumentSections: Array.from(body.user.instrument_sections) don't work (same error), but I bet they're close: both try putting each instrument_sections object into state whereas I need to look within each instrument_sections to the instrument key and extract its pair. If I could somehow do something like body.user.instrument_sections[all].instrument I'd be in business. Perhaps use some forEach() fx or the like?

    – Adam J
    Jan 11 at 16:11














1












1








1







As you can see in your consoleinstrument_sections is an array of objects so to get the first instrument for example you should get it like:
body.user.instrument_sections[0].instrument.

and instead of instrumentSections: body.user.instrument_sections try
instrumentSections: body.user.instrument_sections.slice() or
instrumentSections: Array.from(body.user.instrument_sections)

and let me know if this solves your problem.






share|improve this answer















As you can see in your consoleinstrument_sections is an array of objects so to get the first instrument for example you should get it like:
body.user.instrument_sections[0].instrument.

and instead of instrumentSections: body.user.instrument_sections try
instrumentSections: body.user.instrument_sections.slice() or
instrumentSections: Array.from(body.user.instrument_sections)

and let me know if this solves your problem.







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 4 at 19:36

























answered Jan 4 at 19:20









amirifyamirify

598




598













  • It doesn't seem to work, but it's still progress, thank goodness! After brushing up on the functions you're suggesting, I totally see where you're going. (updating . . .)

    – Adam J
    Jan 11 at 15:41













  • body.user.instrument_sections[0].instrument works to get the first instrument, which is encouraging. instrumentSections: body.user.instrument_sections.slice() and instrumentSections: Array.from(body.user.instrument_sections) don't work (same error), but I bet they're close: both try putting each instrument_sections object into state whereas I need to look within each instrument_sections to the instrument key and extract its pair. If I could somehow do something like body.user.instrument_sections[all].instrument I'd be in business. Perhaps use some forEach() fx or the like?

    – Adam J
    Jan 11 at 16:11



















  • It doesn't seem to work, but it's still progress, thank goodness! After brushing up on the functions you're suggesting, I totally see where you're going. (updating . . .)

    – Adam J
    Jan 11 at 15:41













  • body.user.instrument_sections[0].instrument works to get the first instrument, which is encouraging. instrumentSections: body.user.instrument_sections.slice() and instrumentSections: Array.from(body.user.instrument_sections) don't work (same error), but I bet they're close: both try putting each instrument_sections object into state whereas I need to look within each instrument_sections to the instrument key and extract its pair. If I could somehow do something like body.user.instrument_sections[all].instrument I'd be in business. Perhaps use some forEach() fx or the like?

    – Adam J
    Jan 11 at 16:11

















It doesn't seem to work, but it's still progress, thank goodness! After brushing up on the functions you're suggesting, I totally see where you're going. (updating . . .)

– Adam J
Jan 11 at 15:41







It doesn't seem to work, but it's still progress, thank goodness! After brushing up on the functions you're suggesting, I totally see where you're going. (updating . . .)

– Adam J
Jan 11 at 15:41















body.user.instrument_sections[0].instrument works to get the first instrument, which is encouraging. instrumentSections: body.user.instrument_sections.slice() and instrumentSections: Array.from(body.user.instrument_sections) don't work (same error), but I bet they're close: both try putting each instrument_sections object into state whereas I need to look within each instrument_sections to the instrument key and extract its pair. If I could somehow do something like body.user.instrument_sections[all].instrument I'd be in business. Perhaps use some forEach() fx or the like?

– Adam J
Jan 11 at 16:11





body.user.instrument_sections[0].instrument works to get the first instrument, which is encouraging. instrumentSections: body.user.instrument_sections.slice() and instrumentSections: Array.from(body.user.instrument_sections) don't work (same error), but I bet they're close: both try putting each instrument_sections object into state whereas I need to look within each instrument_sections to the instrument key and extract its pair. If I could somehow do something like body.user.instrument_sections[all].instrument I'd be in business. Perhaps use some forEach() fx or the like?

– Adam J
Jan 11 at 16:11













-1














Me and the guy I worked with aren't sure if it's the most efficient method or if it's kosher with reactjs style, but at long last, the following javascript loop and accessories will work. After .then(response => response.json()):



.then(body => {
const sections = ;
for(let i = 0; i < body.user.instrument_sections.length; i++){ sections.push(body.user.instrument_sections[i].instrument);
};



And change the setState to this:



this.setState({ student: body.user, instrumentSections: sections)}



So what happens is js reaches into the body and through iteration grabs each relevant instrument, which goes into the sections array, and finally you set the state of instrumentSections equal to sections.






share|improve this answer
























  • -1? Uh oh. Did I break etiquette? Is there something particularly wrong with the solution even if it's functional? Something else?

    – Adam J
    Feb 7 at 19:17
















-1














Me and the guy I worked with aren't sure if it's the most efficient method or if it's kosher with reactjs style, but at long last, the following javascript loop and accessories will work. After .then(response => response.json()):



.then(body => {
const sections = ;
for(let i = 0; i < body.user.instrument_sections.length; i++){ sections.push(body.user.instrument_sections[i].instrument);
};



And change the setState to this:



this.setState({ student: body.user, instrumentSections: sections)}



So what happens is js reaches into the body and through iteration grabs each relevant instrument, which goes into the sections array, and finally you set the state of instrumentSections equal to sections.






share|improve this answer
























  • -1? Uh oh. Did I break etiquette? Is there something particularly wrong with the solution even if it's functional? Something else?

    – Adam J
    Feb 7 at 19:17














-1












-1








-1







Me and the guy I worked with aren't sure if it's the most efficient method or if it's kosher with reactjs style, but at long last, the following javascript loop and accessories will work. After .then(response => response.json()):



.then(body => {
const sections = ;
for(let i = 0; i < body.user.instrument_sections.length; i++){ sections.push(body.user.instrument_sections[i].instrument);
};



And change the setState to this:



this.setState({ student: body.user, instrumentSections: sections)}



So what happens is js reaches into the body and through iteration grabs each relevant instrument, which goes into the sections array, and finally you set the state of instrumentSections equal to sections.






share|improve this answer













Me and the guy I worked with aren't sure if it's the most efficient method or if it's kosher with reactjs style, but at long last, the following javascript loop and accessories will work. After .then(response => response.json()):



.then(body => {
const sections = ;
for(let i = 0; i < body.user.instrument_sections.length; i++){ sections.push(body.user.instrument_sections[i].instrument);
};



And change the setState to this:



this.setState({ student: body.user, instrumentSections: sections)}



So what happens is js reaches into the body and through iteration grabs each relevant instrument, which goes into the sections array, and finally you set the state of instrumentSections equal to sections.







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 30 at 22:57









Adam JAdam J

65




65













  • -1? Uh oh. Did I break etiquette? Is there something particularly wrong with the solution even if it's functional? Something else?

    – Adam J
    Feb 7 at 19:17



















  • -1? Uh oh. Did I break etiquette? Is there something particularly wrong with the solution even if it's functional? Something else?

    – Adam J
    Feb 7 at 19:17

















-1? Uh oh. Did I break etiquette? Is there something particularly wrong with the solution even if it's functional? Something else?

– Adam J
Feb 7 at 19:17





-1? Uh oh. Did I break etiquette? Is there something particularly wrong with the solution even if it's functional? Something else?

– Adam J
Feb 7 at 19:17


















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54044237%2fhow-to-fetch-deeply-nested-data-from-the-response-body-into-state%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas