【问题标题】:Problem with getting data from firestore and display it on the screen从firestore获取数据并将其显示在屏幕上的问题
【发布时间】:2021-04-06 03:28:12
【问题描述】:

我正在尝试使用 angular 和 firebase 编写网站。
该站点可以创建不同的用户(我添加了 auth.service.ts) 任何注册用户都可以将联系人添加到 Firestore。

问题:我无法让添加的联系信息出现在屏幕上。

我在控制台中不断遇到的错误是:

core.js:6241 ERROR FirebaseError: Function CollectionReference.doc() 不能用空路径调用。

我的 Firestore 设计:
集合名称“用户”-> 包含每个用户的文档-> 每个用户都有“联系人”集合-> 每个联系人包含电子邮件、姓名、电话联系人属性。

以下代码来自 crud.service.ts 文件:

export class CrudService {
  constructor(private authservice: AuthService, public fireservices:AngularFirestore) { }
  
  //create_NewContact adds to the 'contacts' collection in firebase, the contact that the user entered as input
  create_NewContact(Record)
  {  
    return this.fireservices.collection('users').doc(this.authservice.currentUserId).collection('contacts').add(Record);
  }

  //get_AllContacts gets the 'contacts' collection from firebase
  get_AllContacts()
  {
    return this.fireservices.collection('users').doc(this.authservice.currentUserId).collection('contacts').snapshotChanges();
  }
.
.
.
}

以下代码来自contact-list.component.ts文件:

export class ContactListComponent implements OnInit {
  contact: any;
  contactName: string;
  contactEmail: string;
  contactPhone: string;
  message:string;
  
  constructor(private authservice: AuthService,public crudservice:CrudService) { }
  
  ngOnInit() {
    this.crudservice.get_AllContacts().subscribe(data => {
      this.contact = data.map(c => {
        return {
          id: c.payload.doc.id,
          isEdit: false,
          name: c.payload.doc.data()['name'],
          email: c.payload.doc.data()['email'],
          phone: c.payload.doc.data()['phone'],
        };
      })
      console.log(this.contact);
    });
  }
  
  /*CreateRecord() will fire after the user press the "Create Contact" btn*/
  CreateRecord()
  {
    //The function stores within the relevant fields in "Record" variable, the user's input
    let Record = {};
    Record['name'] = this.contactName;
    Record['email'] = this.contactEmail;
    Record['phone'] = this.contactPhone;
    
    //create_NewContact is defined in crud.service.ts file
    this.crudservice.create_NewContact(Record).then(res => {
      this.contactName = "";
      this.contactEmail = "";
      this.contactPhone = "";
      console.log(res);
      this.message = "Contact data save done";
    }).catch(error => {
      console.log(error);
    })
  }
.
.
.
}

您有解决此问题的想法吗?谢谢!

【问题讨论】:

    标签: javascript angular firebase google-cloud-firestore


    【解决方案1】:

    错误信息表明传递给doc()方法的值是一个空字符串。

    问题很可能来自这样一个事实,即在您调用此方法时,this.authservice.currentUserId 为空。 currentUserId 可能为 null,因为 authservice 对象尚未完成初始化:有关详细信息,请参阅 doc

    您应该使用onAuthStateChanged observer 或检查this.authservice.currentUserId 是否不为空。

    【讨论】:

    • 非常感谢您的回答!我应该在 contact-list.component.ts 文件或 crud.service.ts 文件中使用 onAuthStateChanged 观察者吗? @Renaud Tarnec
    • 如果没有显示所有交互的 Angular 项目的完整地图,很难回答。当登录过程完成时(即没有更多对象处于中间状态),将触发观察者 => 当它被触发并且user 不为空时,您应该调用您的create_NewContactget_AllContacts 方法。
    • 谢谢@Renaud Tarnec!按照您的建议,我检查了 this.authservice.currentUser 不为空:if(this.authservice.currentUser != null)(在contact-list.component.ts 文件中),现在没有错误!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-22
    • 2023-03-07
    • 2021-05-05
    • 2019-02-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多