如何在firebase数据库中进行查询?

时间:2018-07-23 03:02:59

标签: javascript reactjs firebase firebase-realtime-database

我在firebase中具有以下数据结构作为实时数据库:

{
  "react" : {
    "url_01" : "https://stackoverflow.com/",
    "url_02" : "https://google.com/",
    "url_03" : "https://www.youtube.com/"
  }
}

我正在尝试在React中查询数据库以在以下组件中显示所有URL。

到目前为止,我已经知道它可以在数据库中正确显示第一个URL,但是现在尝试将它们全部显示为div <h1>

class FirebaseDB extends React.Component {
  constructor() {
    super();
    this.state = {
      speed: [],
    };
  }

  componentDidMount() {
    const rootRef = firebase.database().ref().child('react');
    const speedRef = rootRef.child('url_01');
    speedRef.on('value', snap => {
      this.setState({
        speed: snap.val()
      });
    });
  }

  render() {
    return (

        <div>
          <h1>URL: {this.state.speed}</h1>
        </div>


    );
  }
}

2 个答案:

答案 0 :(得分:7)

fromEvent(window, 'popstate')
  .subscribe((e) => {
    console.log(e, 'back button');
  });

答案 1 :(得分:0)

另一种解决方案:

const object1 = {
    "url_01" : "https://stackoverflow.com/",
    "url_02" : "https://google.com/",
    "url_03" : "https://www.youtube.com/"
};

let a = Object.values(object1);

a现在是

["https://stackoverflow.com/","https://google.com/","https://www.youtube.com/"]