Reputation: 543
I am trying to save a newly created user's info into Firestore using the uid as the document id. The problem I encounter comes after the creation of the user, in order to save his information into the Firestore collection after creation. Here is what I have already tried:
const usersRef = firebase.firestore().collection('Users');
firebase.auth().createUserWithEmailAndPassword(values.email, values.password).then(function(user){
usersRef.doc(`${user.uid}`).set({
firstName: values.firstName,
lastName: values.lastName,
username: values.username,
uid: user.uid
})
}).catch(function(error){
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
// [START_EXCLUDE]
if (errorCode == 'auth/weak-password') {
alert('The password is too weak.');
} else {
alert(errorMessage);
}
console.log(error);
// [END_EXCLUDE]
});
The code is expected to save the user's info after creation, but it causes the following error : The collection document ID cannot be undefined or so
Upvotes: 1
Views: 1798
Reputation: 20584
The method you're using using returns Promise<UserCredential>
. Your code should be:
const usersRef = firebase
.firestore()
.collection('Users');
firebase
.auth()
.createUserWithEmailAndPassword(values.email, values.password)
.then(function(userCredential) {
usersRef
.doc(`${userCredential.user.uid}`)
.set({
firstName: values.firstName,
lastName: values.lastName,
username: values.username,
uid: userCredential.user.uid
})
...
Upvotes: 7