EDIT: This was made with Nextjs. I created a new vanilla CRA. It works like a charm there. It seems like the problem is related to Nextjs in some way.
I'm trying to create a new user document for containing additional user information, when a new user is created. I have a function which will first create a new user, and then populate a new document in my users collection.
The user gets created, however the users collection does not get populated with a new user document, containing the additional information.
const createUser = (user) => {
fire.auth()
.createUserWithEmailAndPassword(user.email, user.password)
.then((registeredUser) => {
// shows me the newly created user's id.
console.log(registeredUser.user.uid);
fire.firestore().collection('users')
.add({
uid: registeredUser.user.uid,
firstName: user.firstName,
lastName: user.lastName,
}).catch((err) => {
// No errors.
console.log(err);
});
});
};
However, if I do this, where I add fire.firestore().collection('users').add({});
I will get two new documents stored in my users collection, both the empty object from the 'dummy' line, and also the document with the additional user information.
const createUser = (user) => {
// If I'm adding this line, I get both the empty object, and the additional user info
// stored in db.
fire.firestore().collection('users').add({});
fire.auth()
.createUserWithEmailAndPassword(user.email, user.password)
.then((registeredUser) => {
// shows me the newly created user's id.
console.log(registeredUser.user.uid);
fire.firestore().collection('users')
.add({
uid: registeredUser.user.uid,
firstName: user.firstName,
lastName: user.lastName,
}).catch((err) => {
// No errors.
console.log(err);
});
});
};
Can someone explain to me why this is happening? Why doesn't the first block of code work? How can I make the first block of code work, so that I get my additional user fields in the database?
Any help would be much appreciated.
Regards Stephan Valois