【发布时间】:2019-12-18 06:34:00
【问题描述】:
很抱歉,虽然我搜索了解决方案,但我无法解决问题。
我可以获取 res,但无法通过 res.data 访问它。
我尝试使用异步箭头函数来修复它。
class QuizLoader extends Component {
constructor(props) {
super(props);
this.state = {
receivedData: []
}
}
//load data
componentDidMount() {
this.getData();
}
getData = async () => {
const dataBefore = this.state.receivedData;
console.log("receivedData before sending is ");
console.log(dataBefore);
// get Quiz data from database
let res = await axios
.get('https://backend-pflegonaut.firebaseio.com/.json')
.catch(err => {
console.log(err);
return null;
});
let { data } = res.data;
这里我只得到未定义
console.log({res});
见下面的代码
this.setState({receivedData: data});
}
render() {
return (
<div>
{this.state.receivedData.length === 0 ? (
<div>Loading...</div>
) : (
这里的第二个问题:this.state.receivedData.map 不是函数
this.state.receivedData.map((e, i) => {
return <div key={i}>{e.Frage}</div>;
}
也试过了:this.state.receivedData.Object.map
)
)}
</div>
);
}
}
export default QuizLoader;
console.log 中的数据概览:
【问题讨论】:
-
控制台: {res: {…}} res: config: {url: "backend-pflegonaut.firebaseio.com/.json", 方法: "get", headers: {…}, transformRequest: Array(1), transformResponse : Array(1), …} data: {Frage2: {…}, Frage3: {…}, Quiz: {…}} headers: {content-type: "application/json; charset=utf-8", cache-控制:“无缓存”,内容长度:“439”}请求:XMLHttpRequest {onreadystatechange:ƒ,readyState:4,超时:0,withCredentials:false,上传:XMLHttpRequestUpload,...}状态:200 statusText:“OK” proto:对象 proto:对象
-
所以数据在那里:)
-
嗨,Pflegonaut,试试下面我的解决方案,如果有帮助,请告诉我。
-
嘿,感谢您的快速帮助 :) 我输入了 try catch 语句,但不幸的是,'console.log({data})' 仍未定义。
-
我没有足够的帖子来支持它 :) 老实说,我还有另一个问题,它与已解决的问题有关。我随时将其发布在解决方案部分下。
标签: reactjs firebase asynchronous axios