简体   繁体   English

RTK 查询有时会从 Cloud Firestore 中提取旧数据

[英]RTK Query sometimes fetches old data from Cloud Firestore

I'm currently working on a SPA (React, RTK Query with automated refetch) that uses Cloud Firestore as backend.我目前正在开发一个使用 Cloud Firestore 作为后端的 SPA(React,RTK 查询和自动重新获取)。 This is my first time working on a larger project and also using Firebase so I really need some input or advice.这是我第一次从事更大的项目并且还使用 Firebase,所以我真的需要一些意见或建议。

The issue I've run into is that when you're logged into your account on the site and do any type of update on your profile, it'll still show the old data (it disappears as soon as I refresh the site).我遇到的问题是,当您在网站上登录您的帐户并对您的个人资料进行任何类型的更新时,它仍然会显示旧数据(一旦我刷新网站,它就会消失)。

I've been trying to find the cause and in Redux devtools the sequence looks like this:我一直在努力寻找原因,在 Redux devtools 中,序列如下所示:

  1. User does a change用户做了改变
  2. RTK Query fires updateUserCollection mutation and is pending RTK 查询触发 updateUserCollection 突变并且正在等待
  3. When that's fulfilled, it fires getUserData query (pending)完成后,它会触发 getUserData 查询(待处理)
  4. getUserData is fulfilled, but the old data is still on the site getUserData 已完成,但旧数据仍在站点上

When I check getUserData in Redux devtools I can see that it has fetched the old data.当我在 Redux devtools 中检查 getUserData 时,我可以看到它已经获取了旧数据。 The odd thing is that this problem isn't consistent.奇怪的是这个问题并不一致。 After I've refreshed the site and try to do another update, everything will work fine, but every now and then it'll fetch the old data.在我刷新站点并尝试进行另一次更新后,一切都会正常工作,但它会时不时地获取旧数据。 This also happens on other places on the site (for example when the Admin tries to add a new product).这也发生在网站的其他地方(例如,当管理员尝试添加新产品时)。

I've been trying to see if there's a problem with how I've written the RKT Query code, but I'm starting to think that the issue lies with Firebase. Sometimes it looks like it's a bit slower on updating the documents, and I'm wondering if RTK Query is somehow managing to fetch the data before Firebase has had time to update the documents?我一直在尝试查看我的RKT查询代码的编写方式是否存在问题,但我开始认为问题在于Firebase。有时看起来更新文档有点慢,并且我想知道 RTK 查询是否以某种方式设法在 Firebase 有时间更新文档之前获取数据?

I'm thinking of trying to add some sort of time delay to getUserData, but honestly I don't like the idea of trying to force the app to be slower just to accommodate Firebase. It could also be that it's just failing to fetch the data so it uses the old one instead, but I don't know how to verify that.我正在考虑尝试为 getUserData 添加某种时间延迟,但老实说,我不喜欢试图强制应用程序变慢以适应 Firebase 的想法。也可能是它只是无法获取数据所以它使用旧的,但我不知道如何验证。

Any suggestions or ideas on how this problem could be solved?关于如何解决这个问题的任何建议或想法?


(Adding an example code from my slices in case I have done a mistake) (从我的切片中添加示例代码以防我做错了)

getUserData: builder.query({
  async queryFn(uid) {
    try {
      const userQuery = query(
        collection(db, "users"),
        where("userId", "==", uid)
      );
      const querySnapshot = await getDocs(userQuery);
      const data = querySnapshot?.docs[0].data();

      return { data: data };
    } catch (err) {
      return { error: err };
    }
  },
  providesTags: ["Users", "UserData"],
}),
updateUserCollection: builder.mutation({
  async queryFn(data) {
    try {
      const userQuery = query(
        collection(db, "users"),
        where("userId", "==", data[0].userId)
      );
      const querySnapshot = await getDocs(userQuery);

      querySnapshot.forEach(async (doc) => {
        await updateDoc(doc.ref, {
          userCollection: [...data],
        });
      });

      return { data: "edited" };
    } catch (err) {
      return { error: err };
    }
  },
  invalidatesTags: ["Users", "UserData"],
}),

Writing an answer here in case someone else ever has the same issue.在这里写下答案以防其他人遇到同样的问题。

It seems rewriting some of my code in the Get query and Update mutation did the trick.似乎在 Get 查询和 Update 突变中重写了我的一些代码就成功了。 (I also updated RTK Query from 1.8.5 to 1.9.0 before changing the code but didn't notice a difference). (在更改代码之前,我还将 RTK 查询从 1.8.5 更新到 1.9.0,但没有注意到差异)。

Here's how the code looks now after the changes:下面是更改后的代码现在的样子:

getUserData: builder.query({
  async queryFn(uid) {
    try {
      const docRef = doc(db, "users", uid);
      const docSnap = await getDoc(docRef);
      const data = docSnap.data();

      return { data: data };
    } catch (err) {
      return { error: err };
    }
  },
  providesTags: ["UserData"],
}),
updateUserCollection: builder.mutation({
  async queryFn(data) {
    try {
      const docRef = doc(db, "users", data[0].userId);

      await updateDoc(docRef, {
        userCollection: [...data],
      });

      return { data: "updated" };
    } catch (err) {
      return { error: err };
    }
  },
  invalidatesTags: ["UserData"],
}),

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

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