admin管理员组文章数量:1289634
i'm trying to get user UID in the place of auto generated document ID in Firebase/Firestore but can't get it because of this error
TypeError: firebase.auth(...).currentUser is null
this is my index.js file:-
// Firestore Cloud Database
var db = firebase.firestore();
function reg(){
//window.alert("Working..!");
const txtname = document.getElementById('txtuname').value;
const txtEmail = document.getElementById('txtemail').value;
const txtPass = document.getElementById('txtpass').value;
//window.alert(txtname);
firebase.auth().createUserWithEmailAndPassword(txtEmail, txtPass).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]
});
// Getting user id
var uid = firebase.auth().currentUser.uid;
//User Data Insertion
if(uid !=null){
db.collection("users").doc(uid).add({
UserName: txtname,
Email: txtEmail,
Password: txtPass
})
// .then(function(uid) {
// console.log("Document written with ID: ", uid);
// })
.catch(function(error) {
console.error("Error adding document: ", error);
});
}
}
i'm trying to get user UID in the place of auto generated document ID in Firebase/Firestore but can't get it because of this error
TypeError: firebase.auth(...).currentUser is null
this is my index.js file:-
// Firestore Cloud Database
var db = firebase.firestore();
function reg(){
//window.alert("Working..!");
const txtname = document.getElementById('txtuname').value;
const txtEmail = document.getElementById('txtemail').value;
const txtPass = document.getElementById('txtpass').value;
//window.alert(txtname);
firebase.auth().createUserWithEmailAndPassword(txtEmail, txtPass).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]
});
// Getting user id
var uid = firebase.auth().currentUser.uid;
//User Data Insertion
if(uid !=null){
db.collection("users").doc(uid).add({
UserName: txtname,
Email: txtEmail,
Password: txtPass
})
// .then(function(uid) {
// console.log("Document written with ID: ", uid);
// })
.catch(function(error) {
console.error("Error adding document: ", error);
});
}
}
Share
Improve this question
edited Apr 24, 2018 at 10:00
KENdi
7,7812 gold badges18 silver badges31 bronze badges
asked Apr 24, 2018 at 7:53
JM007JM007
331 gold badge1 silver badge3 bronze badges
1
- First a little personal tip: proper code indentation makes it easyer to read (for me). Second it looks like you are trying to add some data in firestore when you create a new user. In that case you have to make sure firebase has finished creating the use: take a look at the docs – André Kool Commented Apr 24, 2018 at 8:13
3 Answers
Reset to default 6Since firebase.auth().createUserWithEmailAndPassword(...)
is an async function, you have to wait for it to resolve before continuing.
You can try this:
// Firestore Cloud Database
var db = firebase.firestore();
function reg(){
//window.alert("Working..!");
const txtname = document.getElementById('txtuname').value;
const txtEmail = document.getElementById('txtemail').value;
const txtPass = document.getElementById('txtpass').value;
//window.alert(txtname);
firebase.auth().createUserWithEmailAndPassword(txtEmail,txtPass)
.then(function (user) {
// insert any document you want here
})
.catch(function(error) {
// handle error here
});
}
Before I present you with my answer, BEWARE cloud functions do take unpredictable amount of time to process. So if you need to immediately start using the data stored in the users collection, maybe it's not the right method for you to take.
Using firebase cloud functions..
exports.createUser = functions.auth.user().onCreate((user) => {
const userMap = {
uid: user.uid,
email: user.email,
};
return admin.firestore().collection('user').doc(user.uid).set(userMap);
});
You can relate the below example and generate your code
Here "_email","_password","_city","_phone"
are variables for
onSaved: (value) => _email = value
taken from TextFormField()
function
Code:
String userId = await widget.auth.createUserWithEmailAndPassword(_email, _password);
print('Registered user: $userId');
final new_user = await FirebaseFirestore.instance.collection('users').doc(userId).
set({"UserUID":'$userId',"Email":_email,"Password":_password,"City":_city,"Phone
Number":_phone});
本文标签: javascriptHow to add user UID in the place of firestore document IDStack Overflow
版权声明:本文标题:javascript - How to add user UID in the place of firestore document ID - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741456649a2379794.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论