Home > Enterprise >  How do I navigate to a certain screen given the presence of a document on firestore
How do I navigate to a certain screen given the presence of a document on firestore

Time:01-28

This process takes place once the user is signed in. Once they are signed in, I want to check if they have a firestore document associated with their uid. If they do have one, send them to the homescreen. If they do not have one , send them to a "complete your profile page". Here is the code I have implemented so far:

/**Library Imports */
import React, { useState, useEffect } from 'react';
import { StyleSheet, Text, View } from 'react-native'
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import { StackActions } from '@react-navigation/native';
import firestore from '@react-native-firebase/firestore';
import AsyncStorage from '@react-native-async-storage/async-storage'
import auth from '@react-native-firebase/auth';

/**Screen Imports */
import HomeScreen from '../Screens/HomeScreen';
import FinishProfileScreen from '../Screens/login/FinishProfileScreen'


const Stack = createNativeStackNavigator();

const AppStack = () => {

  
  let curUser = auth().currentUser;  
  let routeName;
  const userRef = firestore().collection('users');
  


  async function docExists(docName, docId) {
    const docRef = firestore().collection(docName).doc(docId);
  
    let docSnapshot = await docRef.get();
    
    if (docSnapshot.exists) {
      console.log("docExists True")
      routeName = "HomeScreen"
      return true;
    } else {
      console.log("docExists False")
      routeName = "FinishProfileScreen"
      return false;
    }

  }

  docExists("users", curUser.uid)

  console.log(routeName)
  
  return (
    <Stack.Navigator initialRouteName={routeName}>
      <Stack.Screen 
          name = "Home"
          component = {HomeScreen}
        />
      <Stack.Screen 
          name = "FinishProfileScreen"
          component = {FinishProfileScreen}
      />
    </Stack.Navigator>
  )
}

export default AppStack

This keeps navigating me to the homepage when I create a new user. It should notice that there is no firestore documents created and navigate to the FinishProfilePage. Furthermore, my debug console is confirming that the document does not exist, so why am I running into this issue? Thanks in advance!

 LOG  undefined
 LOG  docExists False
 LOG  undefined
 LOG  docExists False
 LOG  undefined
 LOG  docExists False
 LOG  undefined
 LOG  docExists False
 LOG  undefined
 LOG  docExists False

CodePudding user response:

Seems you are calling docExists(docName, docId) and users collections at the same time so the user is undefined before using uid .Check if a user logged-in Firestore document exists using the useEffect hook. The checkDoc function will only be called when curUser changes by passing it in the dependency array of the useEffect hook. This ensures that the check only takes place when a user is logged in. If curUser is changed in any way, the useEffect will also re-render the component.

An example using the useEffect hook looks like the following:

   useEffect(() => {
  async function checkDoc() {
    if(!curUser) return;
    const docRef = firestore().collection("users").doc(curUser.uid);
    let docSnapshot = await docRef.get();
    if (docSnapshot.exists) {
      console.log("docExists True");
      routeName = "HomeScreen"
    } else {
      console.log("docExists False");
      routeName = "FinishProfileScreen"
    }
  }
  checkDoc();
}, [curUser]);
  • Related