屏幕没有更新,但在数据库更新时获取控制台日志

发布于 2025-01-12 02:47:04 字数 1606 浏览 0 评论 0原文

我正在构建后端应用程序,当下订单时,我得到控制台日志,但屏幕没有更新,后退按钮(反应导航)也没有更新,几秒钟后响应。

  const GetdataUsers = async () => {
    let grabbedData1 = [];
          let customerList1 = [];
          await firebase
            .database()
            .ref(`/serviceProvider/${user1.uid}/franchise/customers`)
            .orderByKey()
            .on("value", (snapshot) => {
              customerList1.push(snapshot.val());
              // setCustomerList(customerList1);
              if (customerList1) {
                Object.keys(customerList1).map(function (key) {
                  let y = customerList1[key];
                   Object.keys(y).map(function (key2) {
                      let x = y[key2]; // get all customer id
                      // From that id fetch all the orders they have
                       firebase
                        .database()
                        .ref(`/orders/${x}`)
                        .orderByKey()
                        .on("value", (snapshot, key) => {
                          grabbedData1.push(snapshot.val());
                          console.log("grabbedData1....",grabbedData1);
                          // Grab all orders from the customers and set it
                          setShowloading(false);
                          setOrders(grabbedData1);
                        });
                    })
                });
              }
            });
  };

  useEffect(() => {
    GetdataUsers();
  }, []); // [orders] tried this way also

当在括号中下订单时,如上所示,后退按钮冻结并且没有得到任何更新。还尝试从 useEffect 内的函数和 setorder 返回数据,但陷入循环。

I am building backend app, when the order is placed I get console log but the screen doesn't get updated and also the back button(react navigation), response after few seconds.

  const GetdataUsers = async () => {
    let grabbedData1 = [];
          let customerList1 = [];
          await firebase
            .database()
            .ref(`/serviceProvider/${user1.uid}/franchise/customers`)
            .orderByKey()
            .on("value", (snapshot) => {
              customerList1.push(snapshot.val());
              // setCustomerList(customerList1);
              if (customerList1) {
                Object.keys(customerList1).map(function (key) {
                  let y = customerList1[key];
                   Object.keys(y).map(function (key2) {
                      let x = y[key2]; // get all customer id
                      // From that id fetch all the orders they have
                       firebase
                        .database()
                        .ref(`/orders/${x}`)
                        .orderByKey()
                        .on("value", (snapshot, key) => {
                          grabbedData1.push(snapshot.val());
                          console.log("grabbedData1....",grabbedData1);
                          // Grab all orders from the customers and set it
                          setShowloading(false);
                          setOrders(grabbedData1);
                        });
                    })
                });
              }
            });
  };

  useEffect(() => {
    GetdataUsers();
  }, []); // [orders] tried this way also

When placing orders in bracket as shown above back button freezes and doesn't get any update . Also tried returning the data from function and setorder inside useEffect but stuck in a loop.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不奢求什么 2025-01-19 02:47:04

在第一个查询中,我获取他们拥有的所有客户 ID,其中
"x" (let x = y[key2];) 然后另一个查询该客户的所有订单
(x) 将其存储到订单 (setOrders(grabbedData1);) 并存储到
在应用程序上打印

我在代码中看到的问题:

  1. 您关心 getDataUsers 函数中的数组变化。
  2. 您有一组嵌套的 Firebase 订阅,它们必然依赖于刚刚更新的 customerList
  3. 您不会取消订阅任何 Firebase 快照订阅。

我认为最好将其分成两个单独的操作或效果,一个用于处理用户的 uid 更改以设置当前的 customerList 状态,第二个用于处理当 customerList 状态更新时获取/更新订单状态。

示例:

当用户更改时更新客户列表。

useEffect(() => {
  const unsubscribe = firebase
    .database()
    .ref(`/serviceProvider/${user1.uid}/franchise/customers`)
    .orderByKey()
    .on("value", (snapshot) => {
      setCustomerList(snapshot.val());
    });

  return unsubscribe;
}, [user1.uid]);

当客户列表更新时更新订单。

useEffect(() => {
  const innerSubscriptions = [];

  if (customerList) {
    setOrders([]); // <-- clear orders for new customers list
    Object.values(customerList).forEach((customer) => {
      Object.values(customer).forEach((id) => {
        const unsubscribe = firebase
          .database()
          .ref(`/orders/${id}`)
          .orderByKey()
          .on("value", (snapshot, key) => {
            const order = snapshot.val();
            // Grab all orders from the customers and merge
            setOrders((orders) => [...orders, order]);
          });
        innerSubscriptions.push(unsubscribe);
      });
    });
  }
  return () => {
    innerSubscriptions.forEach((unsubscribe) => unsubscribe());
  };
}, [customerList]);

At first query I fetch all the customers Ids that they have which is
"x" (let x = y[key2];) then another query for all orders that customer
(x) have and then store it to orders (setOrders(grabbedData1);) and to
print on the app

Issues I see with the code:

  1. You care mutating arrays in the getDataUsers function.
  2. You've a set of nested firebase subscriptions that necessarily depend on the just updated customerList.
  3. You don't unsubscribe from any firebase snapshot subscriptions.

I think it would be better to split this into two separate actions, or effects, one to handle the user's uid changing to set the current customerList state, and a second to handle fetching/updating the orders state when the customerList state updates.

Example:

Update the customer list when user changes.

useEffect(() => {
  const unsubscribe = firebase
    .database()
    .ref(`/serviceProvider/${user1.uid}/franchise/customers`)
    .orderByKey()
    .on("value", (snapshot) => {
      setCustomerList(snapshot.val());
    });

  return unsubscribe;
}, [user1.uid]);

Update the orders when customer list updates.

useEffect(() => {
  const innerSubscriptions = [];

  if (customerList) {
    setOrders([]); // <-- clear orders for new customers list
    Object.values(customerList).forEach((customer) => {
      Object.values(customer).forEach((id) => {
        const unsubscribe = firebase
          .database()
          .ref(`/orders/${id}`)
          .orderByKey()
          .on("value", (snapshot, key) => {
            const order = snapshot.val();
            // Grab all orders from the customers and merge
            setOrders((orders) => [...orders, order]);
          });
        innerSubscriptions.push(unsubscribe);
      });
    });
  }
  return () => {
    innerSubscriptions.forEach((unsubscribe) => unsubscribe());
  };
}, [customerList]);
眼眸里的那抹悲凉 2025-01-19 02:47:04

您已经创建了一个异步函数,但没有使用await 关键字。此外,您还必须为加载程序创建一个状态,并有条件地渲染加载程序组件,直到获取所有数据为止。

You have made an async function but you haven't used the await keyword. Also you'll have to create a state for loader and conditionally render a loader component till you have fetched all the data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文