I'm currently working on a React Native project that utilizes Firebase v9.
My goal is to add a user object to my user collection whenever a new user signs up through the sign-up screen. However, I've encountered an issue where the user object is not being added to the database, and there are no errors being thrown.
Below is my firebase.js setup:
import firebase from 'firebase/compat/app';
import 'firebase/compat/auth';
import 'firebase/compat/firestore';
const firebaseConfig = {
apiKey: '',
authDomain: '',
projectId: '',
storageBucket: '',
messagingSenderId: '',
appId: '',
};
let app;
if (firebase.apps.length === 0) {
app = firebase.initializeApp(firebaseConfig);
} else {
app = firebase.app();
}
const db = app.firestore();
const auth = firebase.auth();
export {db, auth};
I can confirm that the connection details are accurate as my firebase authentication is functioning correctly.
Next, here is a snippet of my sign-in screen where I attempt to insert data into the database:
const SignUp = ({navigation}) => {
const [fullName, setFullName] = useState('');
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const createUser = async (email, password) => {
try {
auth
.createUserWithEmailAndPassword(email, password)
.then(function (user) {
var user = auth.currentUser;
logUser(user);
navigation.navigate('Login');
});
} catch (error) {
alert(error);
}
};
async function logUser(user) {
await addDoc(collection(db, 'users'), JSON.parse(JSON.stringify(user)));
}
When attempting to use the addDoc method without JSON parsing, it results in an error stating that Firebase was called with invalid data. This confirms that this portion of the codebase is being executed.
Additionally, I have adjusted the firestore rules section on the Firebase console to allow all requests to pass through:
allow read, write: if true;
Thank you for your assistance.