Redux RTK query not returning an error, but is returning an isError true

Viewed 21

so I asked this question on reddit a few hours ago and no one got back to me. I am working on a project at work and I am not very familiar with RTK query. I wrote a query for my firestore DB and it worked great, however, I am trying to error handle a few things, so users know if something is wrong. Currently, I am just trying to console.log an error message. For those who are familiar with RTK query you are very familiar with destructing your query like this...

const { data, isLoading, isError, error, isSuccess, refetch } = useFetchProductsQuery();

Down below is my query, but for firebase, I purposely plugged in a fake db name, so I prod array is empty. One question I have before going on is, does getDocs not trigger an error if a wrong db name is in place??? I tried it this way and got nothing, so that is why I am forcing the "throw new Erorr"

 async queryFn() {
            try {
                const colRef = collection(db, 'fakeDB');
                const q = query(colRef, orderBy('price'), limit(3));
                const prod: BasicProductData[] = [];
                const productData = await getDocs(q);
                productData.docs.forEach((doc: any) => {
                    const data = { ...doc.data(), id: doc.id } as BasicProductData;
                    prod.push(data);
                });

                if (prod.length < 1) {
                    throw new Error('no data collected');
                } else {
                    return { data: prod };
                }
                
            } catch (err) {
                return { error: err };
            }
        },

Now in my other page where I am calling that useFetchProductQuery, I am just trying to console.log(error). I also console.log(isError) and that came back as true, so I am wondering why I am not getting anything back from error. Also if anyone know more about firebase and why I am not getting an error when getDocs(q) comes back as nothing, that would be great! Thanks to anyone who replies, and if you need more information, I will try t reply as fast as possible. Thnaks!

0 Answers
Related