1
我試圖使用fetch從api獲取數據。控制檯日誌是給我正確的JSON,但我發現了以下錯誤試圖設置狀態時:當設置狀態時,是什麼導致了這種TypeError?
類型錯誤:未定義無法讀取屬性「的setState」(...)
getInitialState() {
return {
checklist: {},
documents: [],
questions: [],
faqs: [],
hospitals: [],
profile: {},
guarantor: {},
}
},
componentDidMount(){
this.fetchUser(1);
this.fetchFaqs();
},
fetchFaqs() {
fetch(FAQ_API)
.then(function(response){
return response.json();
})
.then(function(json){
console.log("faqs: " , json);
this.setState({
faqs: json,
});
})
.catch((error) => {
console.warn(error);
});
},