Javascript 在Firebase中按id获取特定对象/项目

Javascript 在Firebase中按id获取特定对象/项目,javascript,reactjs,firebase,firebase-realtime-database,Javascript,Reactjs,Firebase,Firebase Realtime Database,我目前正在使用Firebase和React构建一个小型web应用程序,但从React客户端获取Firebase中的特定项目时遇到问题 话虽如此,我已经习惯了javascript,其中一个简单的获取可能看起来像: const url = 'www.example.com/api/' const id = '123' fetch(url + id) <---specific .then(res => res.json()) .then(result => this.setS

我目前正在使用Firebase和React构建一个小型web应用程序,但从React客户端获取Firebase中的特定项目时遇到问题

话虽如此,我已经习惯了javascript,其中一个简单的获取可能看起来像:

const url = 'www.example.com/api/'
const id = '123'
fetch(url + id) <---specific
  .then(res => res.json())
  .then(result => this.setState({results: result})
  .catch(err => console.log(err))
我尝试从firebase获取特定对象的一种方法是:

 componentDidMount(props) {
   const ref = firebase.database().ref("items");
   ref.on("value", snapshot => {
    let storiesObj = snapshot.val();
     storiesObj
      .child(this.props.match.params.id)
      .then(() => ref.once("value"))
      .then(snapshot => snapshot.val())
      .catch(error => ({
         errorCode: error.code,
         errorMessage: error.message
       }));

     });
   }
在这种情况下,错误是

任何帮助都将不胜感激,如果有人知道firebase上的任何好文档,请随时发送链接给我


谢谢你

诀窍在于,你不必像你那样先得到所有物品的价值。 您应该找到
ref,然后查找所需的子项,并使用
.on
一次
获取该子项的值

类似于此,基于您的示例代码:

componentDidMount() {
   firebase.database().ref("items");
      .child(this.props.match.params.id)
      .once("value")
      .then(snapshot => snapshot.val())
      .catch(error => ({
         errorCode: error.code,
         errorMessage: error.message
       }));
   }
为了更好的理解,让我们看一下原始代码并试图找出它为什么出错的原因:

componentDidMount(props) {
   // ⬇️ this ref points to ALL items
   const ref = firebase.database().ref("items");

   // ⬇️ here we're asking for the value stored under the above ref
   ref.on("value", snapshot => {
    let storiesObj = snapshot.val();
     /* so firebase gives us what we ask for, storiesObj
      * is probably a huge js object with all the items inside.
      * And since it's just a regular js object, 
      * it does not have a `child` method on it, thus calling .child errors out.
      */
     storiesObj
      .child(this.props.match.params.id)
      .then(() => ref.once("value"))
      .then(snapshot => snapshot.val())
      .catch(error => ({
         errorCode: error.code,
         errorMessage: error.message
       }));

     });
   }

诀窍是,你不必像你做的那样先得到所有项目的价值。 您应该找到
ref,然后查找所需的子项,并使用
.on
一次
获取该子项的值

类似于此,基于您的示例代码:

componentDidMount() {
   firebase.database().ref("items");
      .child(this.props.match.params.id)
      .once("value")
      .then(snapshot => snapshot.val())
      .catch(error => ({
         errorCode: error.code,
         errorMessage: error.message
       }));
   }
为了更好的理解,让我们看一下原始代码并试图找出它为什么出错的原因:

componentDidMount(props) {
   // ⬇️ this ref points to ALL items
   const ref = firebase.database().ref("items");

   // ⬇️ here we're asking for the value stored under the above ref
   ref.on("value", snapshot => {
    let storiesObj = snapshot.val();
     /* so firebase gives us what we ask for, storiesObj
      * is probably a huge js object with all the items inside.
      * And since it's just a regular js object, 
      * it does not have a `child` method on it, thus calling .child errors out.
      */
     storiesObj
      .child(this.props.match.params.id)
      .then(() => ref.once("value"))
      .then(snapshot => snapshot.val())
      .catch(error => ({
         errorCode: error.code,
         errorMessage: error.message
       }));

     });
   }

这是firebase官方文档,与在web上读取和写入数据相关的部分(这也适用于react):很有可能,你已经去过了,但从我的尝试来看,这仍然是firebase最新和最可靠的文档。这是firebase官方文档,与在web上读写数据相关的部分(这也适用于react):很有可能,您已经去过了,但从我尝试过的情况来看,这仍然是firebase最新、最可靠的文档。