Problems accessing JSON data from API in React/Redux with Thunk












0















I'm working with the NHL API in a React/Redux project. I am using Thunk and Axios to make an API call in the action like so:



import axios from "axios";

export function fetchRangersStats() {
return function(dispatch) {
dispatch({ type: "FETCH_RANGERS_STATS"})
axios.get('https://statsapi.web.nhl.com/api/v1/teams/3/stats')
.then((response) => {
dispatch({
type: "FETCH_RANGERS_STATS_FULFILLED",
payload: response.data.stats[0].splits[0]
})
})
.catch((err) => {
dispatch({
type: "FETCH_RANGERS_STATS_REJECTED",
payload: err
})
})
}
}


I am able to retrieve the API data and have it set up in my App.js like this:



import React, { Component } from 'react';
import logo from './logo.svg';
import './App.scss';
import {connect} from 'react-redux';
import { fetchRangersStats } from './actions/fetchRangersStats-action';

class App extends Component {

componentDidMount = () => {
this.props.fetchRangersStats();
}

render() {

console.log(this.props.rangersStats.data.stat);
const stats = this.props.rangersStats.data;

console.log('Type : ' + typeof(this.props.rangersStats.data.stat));

return (
<div className="App">

</div>
);
}
}

const mapDispatchToProps = dispatch => ({
fetchRangersStats: () => dispatch(fetchRangersStats())
})

const mapStateToProps = state => ({
rangersStats: state.fetchRangersStatsReducer
})


export default connect(mapStateToProps, mapDispatchToProps)(App);


This works and I can retrieve the API data just fine but I am having an issue accessing nested object properties and their values. this.props.rangersStats.data gives me this:



enter image description here



If I go one level deeper into the stat object I get via this.props.rangersStats.data.stat it works and I get this:



enter image description here



That works just fine, but when I try to grab data from within the stat object using something like this.props.rangersStats.data.stat.gamesPlayed or any other property within stat I get an undefined error.



Why can't I used dot notation to grab the properties and their values with stat? i.e. gamesPlayed, wins, losses, etc..



What is the proper way to access the data within this.props.rangersStats.data.stat? I'm new to Redux and Thunk so bear with me.



Here is the reducer :



export default function reducer (state = {
data: ,
fetching: false,
fetched: false,
error: null
}, action) {
switch(action.type) {
case "FETCH_RANGERS_STATS": {
return {
...state,
fetching: true
}
}
case "FETCH_RANGERS_STATS_REJECTED": {
return {
...state,
fetching: false,
error: action.payload
}
}
case "FETCH_RANGERS_STATS_FULFILLED": {
return {
...state,
fetching: false,
fetched: true,
data: action.payload
}
}
default: return state;
}
}









share|improve this question

























  • share your reducer code, also add the error

    – Tal Avissar
    Nov 18 '18 at 19:52













  • I added the reducer. The error I keep getting when trying to access anything within stat is an undefined error.

    – Nick Kinlen
    Nov 18 '18 at 20:03


















0















I'm working with the NHL API in a React/Redux project. I am using Thunk and Axios to make an API call in the action like so:



import axios from "axios";

export function fetchRangersStats() {
return function(dispatch) {
dispatch({ type: "FETCH_RANGERS_STATS"})
axios.get('https://statsapi.web.nhl.com/api/v1/teams/3/stats')
.then((response) => {
dispatch({
type: "FETCH_RANGERS_STATS_FULFILLED",
payload: response.data.stats[0].splits[0]
})
})
.catch((err) => {
dispatch({
type: "FETCH_RANGERS_STATS_REJECTED",
payload: err
})
})
}
}


I am able to retrieve the API data and have it set up in my App.js like this:



import React, { Component } from 'react';
import logo from './logo.svg';
import './App.scss';
import {connect} from 'react-redux';
import { fetchRangersStats } from './actions/fetchRangersStats-action';

class App extends Component {

componentDidMount = () => {
this.props.fetchRangersStats();
}

render() {

console.log(this.props.rangersStats.data.stat);
const stats = this.props.rangersStats.data;

console.log('Type : ' + typeof(this.props.rangersStats.data.stat));

return (
<div className="App">

</div>
);
}
}

const mapDispatchToProps = dispatch => ({
fetchRangersStats: () => dispatch(fetchRangersStats())
})

const mapStateToProps = state => ({
rangersStats: state.fetchRangersStatsReducer
})


export default connect(mapStateToProps, mapDispatchToProps)(App);


This works and I can retrieve the API data just fine but I am having an issue accessing nested object properties and their values. this.props.rangersStats.data gives me this:



enter image description here



If I go one level deeper into the stat object I get via this.props.rangersStats.data.stat it works and I get this:



enter image description here



That works just fine, but when I try to grab data from within the stat object using something like this.props.rangersStats.data.stat.gamesPlayed or any other property within stat I get an undefined error.



Why can't I used dot notation to grab the properties and their values with stat? i.e. gamesPlayed, wins, losses, etc..



What is the proper way to access the data within this.props.rangersStats.data.stat? I'm new to Redux and Thunk so bear with me.



Here is the reducer :



export default function reducer (state = {
data: ,
fetching: false,
fetched: false,
error: null
}, action) {
switch(action.type) {
case "FETCH_RANGERS_STATS": {
return {
...state,
fetching: true
}
}
case "FETCH_RANGERS_STATS_REJECTED": {
return {
...state,
fetching: false,
error: action.payload
}
}
case "FETCH_RANGERS_STATS_FULFILLED": {
return {
...state,
fetching: false,
fetched: true,
data: action.payload
}
}
default: return state;
}
}









share|improve this question

























  • share your reducer code, also add the error

    – Tal Avissar
    Nov 18 '18 at 19:52













  • I added the reducer. The error I keep getting when trying to access anything within stat is an undefined error.

    – Nick Kinlen
    Nov 18 '18 at 20:03
















0












0








0








I'm working with the NHL API in a React/Redux project. I am using Thunk and Axios to make an API call in the action like so:



import axios from "axios";

export function fetchRangersStats() {
return function(dispatch) {
dispatch({ type: "FETCH_RANGERS_STATS"})
axios.get('https://statsapi.web.nhl.com/api/v1/teams/3/stats')
.then((response) => {
dispatch({
type: "FETCH_RANGERS_STATS_FULFILLED",
payload: response.data.stats[0].splits[0]
})
})
.catch((err) => {
dispatch({
type: "FETCH_RANGERS_STATS_REJECTED",
payload: err
})
})
}
}


I am able to retrieve the API data and have it set up in my App.js like this:



import React, { Component } from 'react';
import logo from './logo.svg';
import './App.scss';
import {connect} from 'react-redux';
import { fetchRangersStats } from './actions/fetchRangersStats-action';

class App extends Component {

componentDidMount = () => {
this.props.fetchRangersStats();
}

render() {

console.log(this.props.rangersStats.data.stat);
const stats = this.props.rangersStats.data;

console.log('Type : ' + typeof(this.props.rangersStats.data.stat));

return (
<div className="App">

</div>
);
}
}

const mapDispatchToProps = dispatch => ({
fetchRangersStats: () => dispatch(fetchRangersStats())
})

const mapStateToProps = state => ({
rangersStats: state.fetchRangersStatsReducer
})


export default connect(mapStateToProps, mapDispatchToProps)(App);


This works and I can retrieve the API data just fine but I am having an issue accessing nested object properties and their values. this.props.rangersStats.data gives me this:



enter image description here



If I go one level deeper into the stat object I get via this.props.rangersStats.data.stat it works and I get this:



enter image description here



That works just fine, but when I try to grab data from within the stat object using something like this.props.rangersStats.data.stat.gamesPlayed or any other property within stat I get an undefined error.



Why can't I used dot notation to grab the properties and their values with stat? i.e. gamesPlayed, wins, losses, etc..



What is the proper way to access the data within this.props.rangersStats.data.stat? I'm new to Redux and Thunk so bear with me.



Here is the reducer :



export default function reducer (state = {
data: ,
fetching: false,
fetched: false,
error: null
}, action) {
switch(action.type) {
case "FETCH_RANGERS_STATS": {
return {
...state,
fetching: true
}
}
case "FETCH_RANGERS_STATS_REJECTED": {
return {
...state,
fetching: false,
error: action.payload
}
}
case "FETCH_RANGERS_STATS_FULFILLED": {
return {
...state,
fetching: false,
fetched: true,
data: action.payload
}
}
default: return state;
}
}









share|improve this question
















I'm working with the NHL API in a React/Redux project. I am using Thunk and Axios to make an API call in the action like so:



import axios from "axios";

export function fetchRangersStats() {
return function(dispatch) {
dispatch({ type: "FETCH_RANGERS_STATS"})
axios.get('https://statsapi.web.nhl.com/api/v1/teams/3/stats')
.then((response) => {
dispatch({
type: "FETCH_RANGERS_STATS_FULFILLED",
payload: response.data.stats[0].splits[0]
})
})
.catch((err) => {
dispatch({
type: "FETCH_RANGERS_STATS_REJECTED",
payload: err
})
})
}
}


I am able to retrieve the API data and have it set up in my App.js like this:



import React, { Component } from 'react';
import logo from './logo.svg';
import './App.scss';
import {connect} from 'react-redux';
import { fetchRangersStats } from './actions/fetchRangersStats-action';

class App extends Component {

componentDidMount = () => {
this.props.fetchRangersStats();
}

render() {

console.log(this.props.rangersStats.data.stat);
const stats = this.props.rangersStats.data;

console.log('Type : ' + typeof(this.props.rangersStats.data.stat));

return (
<div className="App">

</div>
);
}
}

const mapDispatchToProps = dispatch => ({
fetchRangersStats: () => dispatch(fetchRangersStats())
})

const mapStateToProps = state => ({
rangersStats: state.fetchRangersStatsReducer
})


export default connect(mapStateToProps, mapDispatchToProps)(App);


This works and I can retrieve the API data just fine but I am having an issue accessing nested object properties and their values. this.props.rangersStats.data gives me this:



enter image description here



If I go one level deeper into the stat object I get via this.props.rangersStats.data.stat it works and I get this:



enter image description here



That works just fine, but when I try to grab data from within the stat object using something like this.props.rangersStats.data.stat.gamesPlayed or any other property within stat I get an undefined error.



Why can't I used dot notation to grab the properties and their values with stat? i.e. gamesPlayed, wins, losses, etc..



What is the proper way to access the data within this.props.rangersStats.data.stat? I'm new to Redux and Thunk so bear with me.



Here is the reducer :



export default function reducer (state = {
data: ,
fetching: false,
fetched: false,
error: null
}, action) {
switch(action.type) {
case "FETCH_RANGERS_STATS": {
return {
...state,
fetching: true
}
}
case "FETCH_RANGERS_STATS_REJECTED": {
return {
...state,
fetching: false,
error: action.payload
}
}
case "FETCH_RANGERS_STATS_FULFILLED": {
return {
...state,
fetching: false,
fetched: true,
data: action.payload
}
}
default: return state;
}
}






object redux react-redux axios redux-thunk






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 23 at 9:19









skyboyer

3,66111129




3,66111129










asked Nov 18 '18 at 19:15









Nick KinlenNick Kinlen

112112




112112













  • share your reducer code, also add the error

    – Tal Avissar
    Nov 18 '18 at 19:52













  • I added the reducer. The error I keep getting when trying to access anything within stat is an undefined error.

    – Nick Kinlen
    Nov 18 '18 at 20:03





















  • share your reducer code, also add the error

    – Tal Avissar
    Nov 18 '18 at 19:52













  • I added the reducer. The error I keep getting when trying to access anything within stat is an undefined error.

    – Nick Kinlen
    Nov 18 '18 at 20:03



















share your reducer code, also add the error

– Tal Avissar
Nov 18 '18 at 19:52







share your reducer code, also add the error

– Tal Avissar
Nov 18 '18 at 19:52















I added the reducer. The error I keep getting when trying to access anything within stat is an undefined error.

– Nick Kinlen
Nov 18 '18 at 20:03







I added the reducer. The error I keep getting when trying to access anything within stat is an undefined error.

– Nick Kinlen
Nov 18 '18 at 20:03














1 Answer
1






active

oldest

votes


















0














It seems that data inside your state is defined as an array



state = {
data: ,
fetching: false,
fetched: false,
error: null
}


but you are assigning to the array an object {} (action payload is an object)



data: action.payload


This can explains the error you're getting when trying to grab deeper with dot notation the properties and their values






share|improve this answer
























  • I tried switching data to an object or even an empty string but I still get the same undefined error when trying to go deeper into stat. How could I fix this?

    – Nick Kinlen
    Nov 18 '18 at 20:19











  • in reducer try to data: ...action.payload (object clone) and share the code in codepen so we can debug it

    – Tal Avissar
    Nov 18 '18 at 21:14













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%2f53364548%2fproblems-accessing-json-data-from-api-in-react-redux-with-thunk%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









0














It seems that data inside your state is defined as an array



state = {
data: ,
fetching: false,
fetched: false,
error: null
}


but you are assigning to the array an object {} (action payload is an object)



data: action.payload


This can explains the error you're getting when trying to grab deeper with dot notation the properties and their values






share|improve this answer
























  • I tried switching data to an object or even an empty string but I still get the same undefined error when trying to go deeper into stat. How could I fix this?

    – Nick Kinlen
    Nov 18 '18 at 20:19











  • in reducer try to data: ...action.payload (object clone) and share the code in codepen so we can debug it

    – Tal Avissar
    Nov 18 '18 at 21:14


















0














It seems that data inside your state is defined as an array



state = {
data: ,
fetching: false,
fetched: false,
error: null
}


but you are assigning to the array an object {} (action payload is an object)



data: action.payload


This can explains the error you're getting when trying to grab deeper with dot notation the properties and their values






share|improve this answer
























  • I tried switching data to an object or even an empty string but I still get the same undefined error when trying to go deeper into stat. How could I fix this?

    – Nick Kinlen
    Nov 18 '18 at 20:19











  • in reducer try to data: ...action.payload (object clone) and share the code in codepen so we can debug it

    – Tal Avissar
    Nov 18 '18 at 21:14
















0












0








0







It seems that data inside your state is defined as an array



state = {
data: ,
fetching: false,
fetched: false,
error: null
}


but you are assigning to the array an object {} (action payload is an object)



data: action.payload


This can explains the error you're getting when trying to grab deeper with dot notation the properties and their values






share|improve this answer













It seems that data inside your state is defined as an array



state = {
data: ,
fetching: false,
fetched: false,
error: null
}


but you are assigning to the array an object {} (action payload is an object)



data: action.payload


This can explains the error you're getting when trying to grab deeper with dot notation the properties and their values







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 18 '18 at 20:09









Tal AvissarTal Avissar

5,69532344




5,69532344













  • I tried switching data to an object or even an empty string but I still get the same undefined error when trying to go deeper into stat. How could I fix this?

    – Nick Kinlen
    Nov 18 '18 at 20:19











  • in reducer try to data: ...action.payload (object clone) and share the code in codepen so we can debug it

    – Tal Avissar
    Nov 18 '18 at 21:14





















  • I tried switching data to an object or even an empty string but I still get the same undefined error when trying to go deeper into stat. How could I fix this?

    – Nick Kinlen
    Nov 18 '18 at 20:19











  • in reducer try to data: ...action.payload (object clone) and share the code in codepen so we can debug it

    – Tal Avissar
    Nov 18 '18 at 21:14



















I tried switching data to an object or even an empty string but I still get the same undefined error when trying to go deeper into stat. How could I fix this?

– Nick Kinlen
Nov 18 '18 at 20:19





I tried switching data to an object or even an empty string but I still get the same undefined error when trying to go deeper into stat. How could I fix this?

– Nick Kinlen
Nov 18 '18 at 20:19













in reducer try to data: ...action.payload (object clone) and share the code in codepen so we can debug it

– Tal Avissar
Nov 18 '18 at 21:14







in reducer try to data: ...action.payload (object clone) and share the code in codepen so we can debug it

– Tal Avissar
Nov 18 '18 at 21:14






















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%2f53364548%2fproblems-accessing-json-data-from-api-in-react-redux-with-thunk%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







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()