【问题标题】:How to add user UID in the place of firestore document ID如何在 Firestore 文档 ID 的位置添加用户 UID
【发布时间】:2018-10-04 09:26:00
【问题描述】:

我正在尝试获取用户 UID 来代替 Firebase/Firestore 中自动生成的文档 ID,但由于此错误而无法获取它

TypeError: firebase.auth(...).currentUser is null

这是我的 index.js 文件:-

// 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);
});
}

}

【问题讨论】:

  • 首先有一点个人提示:适当的代码缩进使其更易于阅读(对我而言)。其次,当您创建新用户时,您似乎正在尝试在 Firestore 中添加一些数据。在这种情况下,您必须确保 firebase 已完成创建使用:查看the docs

标签: javascript firebase firebase-authentication google-cloud-firestore


【解决方案1】:

由于firebase.auth().createUserWithEmailAndPassword(...) 是一个异步函数,您必须等待它解析后再继续。

你可以试试这个:

// 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
        });

}

【讨论】:

  • 感谢您的帮助
【解决方案2】:

在我向您展示我的答案之前,请注意云功能确实需要不可预测的时间来处理。因此,如果您需要立即开始使用存储在 users 集合中的数据,那可能不是您采用的正确方法。

使用 Firebase 云功能..

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);
});

【讨论】:

    【解决方案3】:

    您可以关联以下示例并生成您的代码

    这里"_email","_password","_city","_phone"是变量

    onSaved: (value) => _email = value

    取自TextFormField()函数

    代码:

    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});
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-28
      • 2020-10-10
      • 1970-01-01
      • 2020-06-10
      • 2020-12-04
      相关资源
      最近更新 更多