当前位置: 首页 > 知识库问答 >
问题:

从firestore获取数据并在屏幕上显示的问题

洪承天
2023-03-14

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

core.js:6241 ERROR firebaseerror:不能使用空路径调用函数collectionreference.doc()。

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

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

共有1个答案

岑畅
2023-03-14

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

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

您应该使用onAuthStateChanged观察器,或者检查this.authService.currentUserid是否为空。

 类似资料:
  • 问题内容: 我试图获取查找栏的值,只要它发生更改并在下面显示。我正在使用我的seekbar上的方法来调用此方法。 问题答案: 这里有一些额外的帮助: 使用该事件检查搜索栏的进度,您可以将该int返回以在应用程序的其他地方使用它。

  • 谁能引导我过去吗?也许我的效用不好?

  • 问题内容: 我们正在考虑用Java编写一种软件,该软件可以在运行期间记录监控什么内容。您对如何有效捕获监视器输出有任何建议吗? 占用的CPU越少,我们捕获屏幕的频率就越高,并且在不干扰其他正在运行的应用程序的情况下,生成的视频越流畅。 如果复制屏幕捕获部分不太困难,则Java中的现有开放源代码程序将起作用。我希望它不需要调用另一种编程语言。我们会看到。 问题答案: 我不确定是否需要框架。 请查阅该

  • 问题内容: 我在JSP中以表格格式显示数据列表。每列中的一列将具有3个不同的链接。Action类中有3个不同的方法,因此单击链接将触发相应的方法。方法从Action类返回数据后,将根据Action类返回的列表大小追加行数。我正在使用struts迭代器进行迭代并添加。我已经编写了执行该操作的jQuery,并且一切正常。 新的要求是,当我单击第一个链接时,它应该显示记录集。当我再次单击第一个链接时,应