React Native firebase firstore add not working

Viewed 21

i'm trying to create an app with firebase firestore and firebase authentication. when i'm adding a document to users collection with add it's not working. but authentication is working. firestore rules are allowed read and write.

firebase configuration

import { initializeApp } from 'firebase/app'
import {
    initializeAuth,
    getReactNativePersistence
} from 'firebase/auth/react-native';

import AsyncStorage from '@react-native-async-storage/async-storage';
import { apiKeyUrl, apiIdUrl } from './env'


// configuration

const apiKey = apiKeyUrl.apiKey
const apiId = apiIdUrl.apiId



const app = initializeApp(firebaseConfig)

const auth = initializeAuth(app, {
    persistence: getReactNativePersistence(AsyncStorage)
});

export { auth }
 */


import firebase from "firebase/compat/app"
import 'firebase/compat/auth'
import 'firebase/compat/firestore'


import { apiKeyUrl, apiIdUrl } from './env'

const apiKey = apiKeyUrl.apiKey
const apiId = apiIdUrl.apiId

const firebaseConfig = {
    apiKey: apiKey,
    authDomain: ".",
    projectId: ".",
    storageBucket: ".",
    messagingSenderId: ".",
    appId: apiId,
    measurementId: "."
};

// Initialize Firebase and Firestore

let app
if (firebase.apps.length === 0) {
    app = firebase.initializeApp(firebaseConfig);
} else {
    app = firebase.app();
}

const db = app.firestore(app)
const auth = app.auth()

export { db, auth }

and

try {
    await auth.createUserWithEmailAndPassword(email, password)
            .then(_ => {
                db.collection('users').add({
                    userEmail : "aaa",
                    userId : "asdasd",
                    userName : "asdasd"
                })
            })
       }catch (err) {
           console.log("err : " + err)            
        }

catch not throwing anything. firebase version is ^9.6.11

1 Answers

Try this it should work

try {
    await auth.createUserWithEmailAndPassword(email, password)
            .then((user) => {
                db.collection('users')
                   .doc(user.uid)
                   .set({
                    userEmail : "aaa",
                    userId : "asdasd",
                    userName : "asdasd"
                   })
            })
       }catch (err) {
           console.log("err : " + err)            
        }
Related