1
votes

I want to add a user to a firestore database after signing up through firebase with email and password. I get no errors, and the user is created and is visible in firebase auth. However the user is not added to the firestore database.

Does anyone know how to make this work? My code snippet is pasted below.

firebase.initializeApp(config);

const auth = firebase.auth();
const db = firebase.firestore();

auth.createUserWithEmailAndPassword(email, pass).then(cred => {
  const userId = cred.user.uid;
  const userData = {
    firstName: firstName,
    lastName: lastName,
    email: email
  };
  db.collection("users").doc(userId).set(userData).then(() => {
    console.log("User successfully added to the DB!");
  })
  .catch((e) => {
    console.log("Error adding user to the DB: ", e);
  });
}).then(() => {
  console.log('User created! - ' + email);
}).catch(e => {
  console.log(e.message);
  txtErrMsg.classList.remove('hide');
  txtErrMsg.innerHTML = e.message;
});

FYI: I have console logged the parameters (userId and userData), and they appear just fine.

Thanks!

2
Do you get an error? - Ivanka Todorova
No, I don't get any errors - Thomas Michael Plantin

2 Answers

2
votes

I figured out what was wrong with my code.

I wrote another method auth.onAuthStateChanged() which was interfering with my action to write to the database. Since I have two auth files (signup.js and login.js), I decided to only keep this method in login.js and to remove it from signup.js.

Now I can successfully add the user to the DB upon signup (signup.js), and I don't need to add anything to the DB when a user is simply logging in (login.js).

I hope this helps anyone out there experiencing the same problem.

1
votes

I experienced that problem myself. The only way I managed to get this working was to await auth first, and then add the user to firestore:

I have a firebaseConfig.js where I set up the usersCollection:

import firebase from 'firebase';
import 'firebase/firestore';

const firebaseConfig = { 
  apiKey: ......
};
firebase.initializeApp(firebaseConfig);

const auth = firebase.auth();
const db = firebase.firestore();

const usersCollection = db.collection('users');

export { auth, usersCollection };

Then, I use it like this.

const fb = require('../firebaseConfig.js');

....

const cred = await fb.auth
    .createUserWithEmailAndPassword(
        '[email protected]',
        'password123'
    )
    .catch(e => {
      console.log(e.message);
    });

if (cred) {
    console.log({ cred });
    const userId = cred.user.uid;
    const userData = {
        firstName: 'firstName',
        lastName: 'lastName',
        email: 'email',
    };

    fb.usersCollection
      .doc(userId)
      .set(userData)
      .then(() => {
        console.log('User successfully added to the DB!');
      })
      .then(() => {
        console.log('User created!);
      })
      .catch(e => {
        console.log('Error adding user to the DB: ', e);
      });
}

When I ran this code I got the following in my log: from the firefox log

And it appears in my database like this: database entry