【发布时间】:2020-05-17 17:08:59
【问题描述】:
有谁知道如何从 firebase 数据库中获取唯一键值?我想将其存储在 state 中并将其打印到 console.log 中。
这是代码,我只是想在控制台中显示它
import React, { Component } from "react";
export class Test extends Component {
constructor(props) {
super(props);
this.state = {
newId: ""
};
}
componentDidMount() {
fetch("https://redditclone-project.firebaseio.com/data.json", {
method: "get",
headers: {
"Content-Type": "application/json",
Accept: "application/json"
}
})
.then(res => res.json())
.then(res => {
console.log(res);
});
console.log('value key, ex:-LzpvyLJnKgDllnTE-eW');
}
render() {
return <div></div>;
}
}
export default Test;
谢谢
编辑:内部资源 并添加了我的 firebase 链接
【问题讨论】:
-
你从
console.log(res);得到什么输出? -
我还注意到您分享的 JSON 屏幕截图中没有
linkfirebase.json。 -
已编辑,还加上 console.log(res)
标签: javascript reactjs firebase firebase-realtime-database fetch