【问题标题】:Firestore auth, use user UID to doc id with ionicFirestore auth,使用用户 UID 与 ionic 记录 id
【发布时间】:2019-09-30 09:25:06
【问题描述】:

当用户注册时,我正在尝试使用用户 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();
              });
  }

目前,当新注册时 -> 它会生成一个 doc id,但是如何使用用户 uid 作为 doc id

感谢您的帮助

【问题讨论】:

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


    【解决方案1】:

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

    将其放入firestore
    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)
     });
    

    【讨论】:

    • 但是如何获得iud,因为这是用户注册的时候
    • 你能说说你注册的方法吗
    • signup() { this.afAuth.auth .createUserWithEmailAndPassword(this.email, this.pass)
    • 我在 console.log 中得到了很好的令牌,但是 this.firestore.collection('users').doc(uid).set(record);在另一个文件(service.ts)中,所以如何将“(iud)”从“this.UsersService.create_NewStudent(record).then(resp => {”设置为service.ts 谢谢
    • 所以只需在 create_NewStudent(iud,record) 中添加另一个参数
    猜你喜欢
    • 2019-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-10
    • 2021-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多