I am having an issue while getting JSON data.
Code having the issue is:
constructor(props, context) {
super(props);
this.state = {anchorEl: null,
showJoin: false,
classDetailsInfo: {
classpic: [],
reviews: []
},
onError: false
}
};
componentDidMount = () => {
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const id = urlParams.get('id')
console.log("this is my id: " + id)
if(urlParams.has('id') && (id !== '')) {
this.setState({userInfo: ClassDataUseCase.getClassDetails(id)})
this.setState({onError: false})
console.log(this.state.userInfo)
} else {
this.setState({onError: true})
}
}
the ClassDataUseCase
is just a "abstraction layer to grab the proper data.
class ClassDataUseCase {
static getClassDetails(id) {
var jsonClasses = JSON.parse(JSON.stringify(ClassDetailsData.Classes));
console.log(jsonClasses)
for(let k=0;k< jsonClasses.length;k++){
if(jsonClasses[k].id === id){
console.log(jsonClasses[k])
return jsonClasses[k];
}
}
console.log("We are f***ed up")
}
}
The JSON is properly returned but when I try to check the content of the this.state.classDetailsInfo
, I just got undefined
in the console. I thought at first that I have to run through a JSON.parse
but it's failing as well.
I am probably not saving it properly in the state.