有人知道如何从firebase数据库中获取唯一键值吗?我想将其存储在状态中,并将其打印到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;

谢谢
编辑:在res内部添加了我的firebase链接
发布于 2020-02-01 12:29:33
如果只想打印JSON中的密钥,可以使用Object.keys()
.then(res => res.json())
.then(res => {
console.log(Object.keys(res));
});由于Object.keys()返回一个数组,因此您还可以使用例如Array.forEach()来遍历这些键。
https://stackoverflow.com/questions/60013908
复制相似问题