Home > database >  Rtk query invalidate tags not invalidating data
Rtk query invalidate tags not invalidating data

Time:08-21

So i have this twitter clone where when i follow a user the posts displayed on the index page should refresh and then show the posts from the user i followed and the follow people list should also refresh removing that user. So i have added invalidate tag for the followUser function invalidating both the posts and users data but it is not working. Only when i refresh do i get the updated data. How do i get it so that without refreshing my posts list and user list are refetched and displays new data.

apiSlice followUser

 followUser:builder.mutation({
    async queryFn({id,currUserId}):Promise<any>{
      try{
        let currUserDocRef = doc(db,`users/${currUserId}`);
        updateDataFirebase(currUserDocRef,'following',id)

        return {data:'ok'} 
      }

      catch(err){
        return {error:err}
      }

    },invalidatesTags:['Posts','Users']
  })

getPosts

getPosts: builder.query<Posts[],void>({
      async queryFn(currUserId):Promise<any>{
        try{
          let followingsArr=await getFollowingArrFirebase(currUserId);
         //console.log(followingsArr)
          let tweetsArr: { }[]=[];
          const q=query(collection(db,'tweets'), where("creatorId", "in" , followingsArr))   
          tweetsArr=await getDataFirebase(q)
          return { data:tweetsArr }
        }

        catch(err:any){
          return{error:err} 
        }

    },providesTags: ['Posts']}),

CodePudding user response:

Usually this happens when people forget to set up the middleware. Can you please check your store setup and validate that you added your apis middleware? If you are using the development build, you should also get warnings on the console about that.

CodePudding user response:

solved it. i was not using await before a function call which returned a promise

  • Related