0

When I console.log(docSnap) I'm getting a firebase error, see in the below image. I have tried all the solutions but none of them worked.

Firebase error

useEffect(() => {
        if (folderId === null) {
            dispatch({
                type: "UPDATE_FOLDER",
                payload: { folder: ROOT_FOLDER }
            })
        }
        // otherwise fetch from database

        const getdocSnap = async () => {
            const docRef = doc(db, "folders", folderId);
            return await getDoc(docRef);
        }
        const docSnap = getdocSnap();
        console.log(docSnap);

        if (docSnap.exists()) {
            dispatch({
                type: "UPDATE_FOLDER",
                payload: { folder: { id: docSnap.id, ...docSnap.data() } }
            })
        } else {

            console.log("No such document!");
            dispatch({
                type: "UPDATE_FOLDER",
                payload: { folder: ROOT_FOLDER }
            })
        }

    }, [folderId])

5
  • Can you console.log(db, folderId) in that function and share a screenshot of the output? Commented May 2, 2022 at 18:25
  • @Dharmaraj the problem has been solved I needed to put all my Firebase-related code to a single async function. Commented May 3, 2022 at 13:18
  • @Gulshan Aggarwal: Could you please post your comment as an answer to help users experiencing the same issue you had? Commented May 9, 2022 at 20:55
  • @AndresFiescoCasasola sure! Commented May 11, 2022 at 7:27
  • @AndresFiescoCasasola solution is little bit longer so I posted it as the answer. Commented May 11, 2022 at 7:37

1 Answer 1

1

How the problem solved ?

Ans: Created an async function callFunc & pasted all my logic inside it & called it.

Try the code below

useEffect(() => {
        if (folderId === null) {
            return dispatch({
                type: "UPDATE_FOLDER",
                payload: { folder: ROOT_FOLDER }
            })
        }
        // otherwise fetch from database
        const callFunc = async () => {
            const docRef = doc(db, "folders", folderId);
            const docSnap = await getDoc(docRef);

            if (docSnap.exists()) {
                dispatch({
                    type: "UPDATE_FOLDER",
                    payload: { folder: { id: docSnap.id, ...docSnap.data() } }
                })
            } else {

                console.log("No such document!");
                dispatch({
                    type: "UPDATE_FOLDER",
                    payload: { folder: ROOT_FOLDER }
                })
            }
        }

        callFunc();

    }, [folderId])

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.