1

I am using react-native 0.81.4 CLI along with the following Firebase packages:

"@react-native-firebase/app": "^23.5.0",
"@react-native-firebase/firestore": "^23.5.0",
"@react-native-firebase/messaging": "^23.5.0",

My code:

import {
  useCallback
} from 'react';
import {
  Alert
} from 'react-native';
import {
  getFirestore,
  collection,
  doc,
  setDoc,
  serverTimestamp,
} from '@react-native-firebase/firestore';
import {
  getApp
} from '@react-native-firebase/app';
import {
  TUser
} from '@/typescript/types/user';

const useSaveUser = () => {
  const onGetUserById = useCallback(async (userId: string, userPayload: Partial < TUser > ) => {
    try {
      console.log('Saving user to Firestore:', userId, userPayload);
      const db = getFirestore(getApp()); // modular way
      console.log('Modular way crossed');
      const userDocRef = doc(collection(db, 'users'), userId);
      console.log('Document reference has been created');

      await setDoc(userDocRef, {
        ...userPayload,
        createdAt: serverTimestamp(),
      });

      console.log(' User saved successfully for ID:', userId);
      Alert.alert('User saved successfully');
    } catch (error) {
      console.error(' Firestore Save Error:', error);
      Alert.alert("Unable to save user's information");
    }
  }, []);

  return {
    onGetUserById
  };
};

export default useSaveUser;

Usage:

const {
  onGetUserById
} = useSaveUser();
const [isLoading, setIsLoading] = useState(false);

const handleSubmit = async () => {
  try {
    setIsLoading(true);
    const userPayload = {
      id: 'id as string',
      firstName: 'givenName as string',
      lastName: 'familyName as string',
      email: 'email as string',
      photo: 'photo as string',
    };
    console.log('just before');
    const response = await onGetUserById('1', userPayload);
    console.log({
      response
    });

  } catch (e) {
    console.log('Sign-In error:', e);
  } finally {
    setIsLoading(false);
  }
};

return ( <Pressable disabled = {
    isLoading
  }
  onPress = {
    handleSubmit
  }>
  <Text>Continue</Text> 
  </Pressable>
)

Logs:

Logs

The main issue I am facing is that when I trigger the Firebase function, my code gets stuck on the save method, and I don't get an error, success or even a timeout!

5
  • Please check now @FrankvanPuffelen Commented Nov 11 at 16:12
  • Why you log console.log({response}); instead of console.log(response); ? Commented Nov 12 at 8:40
  • That does not make any difference! @MichaelBahl Commented Nov 12 at 8:41
  • Did you try to remove the useCallback hook ? Commented Nov 12 at 8:45
  • Yes, it didn't work! Commented Nov 12 at 9:02

0

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.