简体   繁体   English

如何从实时数据库中获取数据以从父母到孩子做出反应?

[英]How to get data from realtime database to react from parents to child?

I'm working on e-commerce website in react js and use Firebase Realtime Database as backend .我在 react js的电子商务网站上工作,并使用Firebase 实时数据库作为后端 i'm trying to get datas from RTDB and want to show on react js website.我正在尝试从 RTDB 获取数据并希望在 React js 网站上显示。

here is my RTDB structure image(1)这是我的 RTDB 结构图 (1) 在此处输入图像描述

this is the data that i stored..(image(2))这是我存储的数据..(图片(2)) 在此处输入图像描述

In above image(1), i stored my Parent node value (/Pant/Formal Pant(Blue Line) -> into caac442a-66fe-ba7e(Red line)).在上图 (1) 中,我存储了我的父节点值(/Pant/Formal Pant(蓝线)-> 到 caac442a-66fe-ba7e(红线))。 I did this from both node (Pant and Shirt).我从两个节点(裤子和衬衫)都这样做了。 i'm able to get that data by manually type my path but not able to get data by dynamically.我可以通过手动输入我的路径来获取该数据,但无法动态获取数据。

Here is my code for read data from realtime databse.这是我从实时数据库读取数据的代码。

// read data
const [itemData, setItemData] = useState([]);
const readData = () => {
  onValue(ref(database, `/products/Shirt/Formal Shirt`), (snapshot) => {
    setItemData([]);
    const data = snapshot.val();
    if (data !== null) {
      const objectData = Object.values(data);
      console.log(objectData);
      setItemData(objectData);
    }
  });
};

try to map data in this...尝试 map 中的数据...

/* -----trying to read data here----- */
{itemData.map((item) => {
  return (
    <div className="item-category-data-list">
      <div className="item-type">
        <h2>{item.category}</h2>
        <h3>view all</h3>
        <div className="item-container-list">
          <div
            className="item-data-container"
            key={item.uuID}
            onClick={() => updateData(item)}
          >
            <img className="item-photo" src={item.image1} alt=""></img>
            <div className="item-data">
              <p className="name">{item.productName}</p>
              <p className="style">{item.type}</p>
              <p className="item-price-row">
                <p className="item-discount-price">
                  ₹{item.discountPrice}
                </p>
                <p className="item-original-price">
                  ₹{item.originalPrice}
                </p>
                <p className="item-discount">({item.discount})</p>
              </p>
              <div className="rating-section">
                <FontAwesomeIcon className="star-icon" icon={faStar} />
                <p className="rating">4.7</p>
                <p className="rating-people">(8.4K)</p>
              </div>
              <p className="item-id">ID: {item.uuID}</p>
            </div>
          </div>
        </div>
      </div>
    </div>
  );
})}

I want to map this data and want to read in my web site.我想要 map 这个数据,想在我的 web 站点阅读。 Please give me some solution to solve this problem.请给我一些解决方案来解决这个问题。

I'm not sure what you mean by "getting path dynamically", but if you want to read all products - you can do that by reading from products , and then navigating the resulting data snapshot.我不确定“动态获取路径”是什么意思,但是如果您想阅读所有产品 - 您可以通过阅读products来做到这一点,然后导航生成的数据快照。

Something like this:像这样:

onValue(ref(database, `/products`), (snapshot) => {
  setItemData([]);
  snapshot.forEach((categorySnapshot) => {
    categorySnapshot.forEach((productSnapshot) => {
      const data = productSnapshot.val();
      if (data !== null) {
        const objectData = Object.values(data);
        console.log(objectData);
        setItemData(objectData);
      }
    }
  }
});

If you want to store all items as an array, that'd be:如果你想将所有项目存储为一个数组,那就是:

onValue(ref(database, `/products`), (snapshot) => {
  setItemData([]);
  let items = [];
  snapshot.forEach((categorySnapshot) => {
    categorySnapshot.forEach((productSnapshot) => {
      const data = productSnapshot.val();
      if (data !== null) {
        const objectData = Object.values(data);
        items.push(objectData);
      }
    })
  })
  setItemData(items);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM