为什么创建文档功能对我不起作用?Firebase/Angular /Firestore

nfs0ujit  于 2022-11-17  发布在  Angular
关注(0)|答案(2)|浏览(132)

我试图提交用户数据到firebase的firestore数据库,但功能,以创建一个新的收集是不为我工作,我已经检查了一些不同的方式来做,但没有一个主题是工作,我已经更新我的firebase配置文件使用我的终端上的firebase命令。
下面是调用使用firestore的服务的代码:

this.authSvc.register(email, password).then((result) => {
      this.authSvc.logout();
      this.verifyEmail();
      this.res = result?.user;
      this.registerDB();
    
    })
    .catch((error) => {
        this.toastr.error(this.firebaseError.codeError(error.code), 'Error');
        this.loading = false;
    });

     
  }

  verifyEmail() {
    this.afAuth.currentUser.then(user => user?.sendEmailVerification())
    .then(() => {
      this.toastr.info('Se envio un correo con el link de verificación', 'Verificar Email')
      this.router.navigate(['/verify-email'])
    });
  }

  async registerDB() {
    const path = 'Users';
    const id = this.res.uid;
    this.userData.uid = id;
    this.userData.password = null;
    await this.frservice.createDoc(this.userData, path, id);
  }

这是firestore服务的代码:

import { Injectable } from '@angular/core';
import { AngularFirestore } from '@angular/fire/compat/firestore';

@Injectable({
  providedIn: 'root'
})
export class FirestoreService {

  constructor(private firestore: AngularFirestore) { }

  createDoc(userData: any, path: string, id: string) {

    const collection = this.firestore.collection(path);
    return collection.doc(id).set(userData);

  }

  getId() {
    return this.firestore.createId();
  }

  getCollection<tipo>(path: string) {

    const collection = this.firestore.collection<tipo>(path);
    return collection.valueChanges();
  }

  getDoc<tipo>(path: string, id: string) {
    return this.firestore.collection(path).doc<tipo>(id).valueChanges()
  }

}

我只想创建一个新的集合,用户的数据将被注册。

ttisahbt

ttisahbt1#

你应该在this.registerDB();调用之前放一个await。别忘了让你的箭头函数async

7dl7o3gd

7dl7o3gd2#

我解决了这个问题,这是firestore数据库的规则:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if false;
    }
  }
}

我把它改成这样的规则:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if request.auth != null;
    }
  }
}

问题是我没有写的权限。

相关问题