【发布时间】: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