firebase n value只能获取一次数据,并且将其放置在React效果中的循环中时不会更新

发布于 2025-02-13 03:55:44 字数 1655 浏览 0 评论 0原文

我的目标是根据客户要求的数据列表获取多个数据,以便我将代码放入使用效果。如果该数组包含客户想要的内容列表,则从服务器中获取这些数据,以便用户可以操纵它。到目前为止,它运行良好,但是当数据库更新时,没有触发OnValue来获取新数据以更新渲染。

这是我的代码。感谢您提前帮助我。

  // Getting data
  useEffect(() => {
    if (empDataArr.length > 1) {
      let fromDay = parseInt(dateHandler(startDate).dateStamp);
      let toDay = parseInt(dateHandler(endDate).dateStamp);
      let tempLogArr = [];
      empDataArr.forEach((emp) => {
        let qLogEvent = query(child(shopRef(shopId), emp.id + "/log_events"), orderByChild("dateStamp"), startAt(fromDay), endAt(toDay));
        
        // This is the part I need help
        onValue(qLogEvent, (snap) => {
          let logEventArr = [];
          let val = snap.val();
          if (val === null) {
          } else {
            Object.keys(val).forEach((key) => {
              let id = key;
              let dateStamp = val[key].dateStamp;
              let direction = val[key].direction;
              let time = val[key].timeStamp + "";
              let timeStamp = time.substring(8, 10) + ":" + time.substring(10, 12);
              logEventArr.push({ direction: direction, timeStamp: timeStamp, dateStamp: dateStamp, id: id });
            });
            tempLogArr.push({
              id: emp.id,
              logEvent: logEventArr,
            });
          }
        });
      });
      setLogDataArr(tempLogArr.map((x) => x));
    }
  }, [empDataArr, shopId, startDate, endDate]);

  useEffect(() => {
    console.log(logDataArr);
  }, [logDataArr]);

我尝试使用return onValue()const logdata = onvalue(),但它们不起作用(我不希望前者也可以使用)。

My goal is to get multiple data based on a list of data the customer requested so I put the codes inside useEffect. If the array contains the list of things the customer wants, then it grab those data from the server so the user can manipulate it. So far, it works fine but when the database updates, onValue is not triggered to grab the new data to update the render.

Here is my code. Thank you for helping me in advance.

  // Getting data
  useEffect(() => {
    if (empDataArr.length > 1) {
      let fromDay = parseInt(dateHandler(startDate).dateStamp);
      let toDay = parseInt(dateHandler(endDate).dateStamp);
      let tempLogArr = [];
      empDataArr.forEach((emp) => {
        let qLogEvent = query(child(shopRef(shopId), emp.id + "/log_events"), orderByChild("dateStamp"), startAt(fromDay), endAt(toDay));
        
        // This is the part I need help
        onValue(qLogEvent, (snap) => {
          let logEventArr = [];
          let val = snap.val();
          if (val === null) {
          } else {
            Object.keys(val).forEach((key) => {
              let id = key;
              let dateStamp = val[key].dateStamp;
              let direction = val[key].direction;
              let time = val[key].timeStamp + "";
              let timeStamp = time.substring(8, 10) + ":" + time.substring(10, 12);
              logEventArr.push({ direction: direction, timeStamp: timeStamp, dateStamp: dateStamp, id: id });
            });
            tempLogArr.push({
              id: emp.id,
              logEvent: logEventArr,
            });
          }
        });
      });
      setLogDataArr(tempLogArr.map((x) => x));
    }
  }, [empDataArr, shopId, startDate, endDate]);

  useEffect(() => {
    console.log(logDataArr);
  }, [logDataArr]);

I have tried using return onValue() and const logData = onValue() but they do not work (and I do not expect the former one to work either).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文