UPDATE: I encountered an issue while using Nextjs framework. However, it works perfectly fine when I use a vanilla CRA (Create React App). It appears that the problem is somehow related to Nextjs.
I'm currently working on creating a new user document to store additional user information whenever a new user is registered. I have a function in place that first creates a new user and then attempts to populate a new document in my users collection.
Although the user is successfully created, the users collection doesn't get populated with the new user document containing the extra information.
const createUser = (user) => {
fire.auth()
.createUserWithEmailAndPassword(user.email, user.password)
.then((registeredUser) => {
// Displays the ID of the newly created user.
console.log(registeredUser.user.uid);
fire.firestore().collection('users')
.add({
uid: registeredUser.user.uid,
firstName: user.firstName,
lastName: user.lastName,
}).catch((err) => {
// No errors are being thrown here.
console.log(err);
});
});
};
However, by including
fire.firestore().collection('users').add({});
I end up with two new documents saved in my users collection - one empty object from the 'dummy' line and the other with the additional user information.
const createUser = (user) => {
// When I include this line, both the empty object and the additional user info
// are stored in the database.
fire.firestore().collection('users').add({});
fire.auth()
.createUserWithEmailAndPassword(user.email, user.password)
.then((registeredUser) => {
// provides me with the ID of the newly registered user.
console.log(registeredUser.user.uid);
fire.firestore().collection('users')
.add({
uid: registeredUser.user.uid,
firstName: user.firstName,
lastName: user.lastName,
}).catch((err) => {
// No errors occur here.
console.log(err);
});
});
};
Can someone offer insight into why this behavior occurs? Why does the initial block of code not function as expected? How can I adjust the initial block of code to ensure that my additional user fields are correctly stored in the database?
Your assistance would be greatly appreciated.
Sincerely, Stephan Valois