首页 > 解决方案 > Firestore auth,使用用户 UID 与 ionic 记录 id

问题描述

当用户注册时,我正在尝试使用用户 UID 作为文档的 ID。

在 service.ts 中,我有这一行:

create_NewStudent(record) {
  return this.firestore.collection('users').add(record);
}

这是我的注册功能

signup() {

     this.afAuth.auth
          .createUserWithEmailAndPassword(this.email, this.pass)
          .then( token => {
      console.log(token.user.uid)


    });


    let record = {};
  record['nom'] = this.nom;
  record['prenom'] = this.prenom;
  record['email'] = this.email;
  record['pass'] = this.pass;
  record['datenaissance'] = this.datenaissance;
  record['lieunaissance'] = this.lieunaissance;
  record['adresse'] = this.adresse;
  record['codepostal'] = this.codepostal;
  record['ville'] = this.ville;
  record['tel'] = this.tel;
  record['secu'] = this.secu;
  record['equipe'] = this.equipe;
  record['poste'] = this.poste;
    this.UsersService.create_NewStudent(uid,record).then(resp => {
  this.nom = "";
  this.prenom = "";
  this.email = "";
  this.pass = "";
  this.datenaissance = "";
  this.lieunaissance = "";
  this.adresse = "";
  this.codepostal = "";
  this.ville = "";
  this.tel = "";
  this.secu = "";
  this.equipe = "";
  this.poste = "";
      console.log(resp);
    })

.then(() => {
               this.modalCtrl.dismiss();
              });
  }

现在,当新注册时 -> 它会生成一个文档 ID,但是如何使用用户 uid 作为文档 ID

在此处输入图像描述

谢谢你的帮助

标签: javascriptionic-frameworkfirebase-authenticationgoogle-cloud-firestore

解决方案


使用 add() 将在随机生成的 id 下创建新文档。假设您有办法获取您的用户 ID。您可以使用

this.firestore.collection('users').doc(uid).set(record);

在注册时获取 uid

 signup() {

     this.afAuth.auth
      .createUserWithEmailAndPassword(this.email, this.pass).then( token => {
      console.log(token.user.uid)
 });

推荐阅读