我正在嘗試學習firebase。我正在使用 Angular 和 Firebase 制作像 Linkedin 這樣的專案。
我在我的專案中使用 @angular/fire 包。
到目前為止,我已經完成了身份驗證并添加了一些用戶的基本資訊。我有users
收藏,其中每個documentId
都有資訊name
,email
等等
現在我的下一個目標是創建一個作業體驗版塊。由于一個用戶可以有多個作業經驗。
我必須決定繼續在每個用戶檔案 id 下進行子集合,而不是為work-experience
.
現在我有點麻煩如何在我的收藏中添加子users
收藏。
用戶一次只能添加/更新一種體驗。我的 UI 將類似于 Linkedin。當單擊“添加體驗”按鈕時,會彈出一個模式,并且在表單內會有一個表單,其中包含諸如等欄位jobTitle
。companyName
因此,一旦單擊提交,我想將該資料保存在work-experience
子集合下,并帶有唯一的檔案 ID。
目前我正在添加這樣的基本資訊
添加
addUser(user: any): Observable<void> {
const ref = doc(this.firestore, 'users', user.uid);
return from(setDoc(ref, user));
}
更新
updateUser(user: any): Observable<void> {
const ref = doc(this.firestore, 'users', user.uid);
return from(updateDoc(ref, { ...user }));
}
現在我想添加在集合work-experience
下呼叫的子集合users
。
uj5u.com熱心網友回復:
從您的另一個問題來看,我認為您需要添加一個包含 ID 檔案的子集合work-experience
。
如果我的假設是正確的,請執行以下操作來創建DocumentReference
此檔案:
const workExperienceDocRef = doc(this.firestore, `users/${user.uid}/sub-sections/work-experience`);
注意:確保使用反引號。
然后可以如下設定這個doc的資料,例如:
return from(setDoc(workExperienceDocRef, {experiences: [{years: "2015-2018", company: "comp1"}, {years: "2018-2021", company: "comp2"}]}));
如果要為用戶的檔案創建子集合,請執行以下操作:
const userSubCollection = collection(this.firestore, `users/${user.uid}/sub-collection`);
然后就可以使用該addDoc()
方法,它會自動生成檔案ID:
const docRef = await addDoc(userSubCollection, {
foo: "bar",
bar: "foo"
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/468493.html
標籤:javascript 有角度的 火力基地 谷歌云火库 nosql
上一篇:如何獲取所有其他單選按鈕的檢查值
下一篇:異步查找最高數