Angular Firebase - 如何将 属性 添加到 Firestore 中的文档?

Angular Firebase - How to add a property to document in firestore?

我对 angular firebase 解决方案有疑问。

我想做的是添加 属性 到 firestore 文档中已经存在的内容。

我有这样的用户界面

export interface User {
    email: string;
    uid: string;
    photoURL?: string;
    displayName?: string;
    teams?: string[];
}

我想创建一个团队并将该团队 ID 添加到现有用户对象中的 teams 数组。

我不想获取所有用户数据,而只想为该字段输入一个新值。

这可能吗?也许您有一些关于如何更有效地做到这一点的建议?

团队界面是这样的

export interface Team {
    name: string;
    description?: string;
    admin?: string; // = creator - first user
    members?: string[]; // array of users UIDs.
    boards?: string[]; // array of board docs id.
} 

负责添加团队的组件只是一个表单,所以 `onSubmit()' 我已经尝试执行以下操作,但我不知道如何将该团队文档 ID 放入用户对象。

onSubmit(form: NgForm) {
    const newTeam: Team = {
        name: form.value.name,
        description: form.value.description,
        admin: this.auth.currentUserId,
        members: [this.auth.currentUserId],
    };
    const userRef: AngularFirestoreDocument<any> = this.afs.doc(
        `users/${this.auth.currentUserId}`
    );
    let teamRefId = '';
    this.afs.collection('teams').add(newTeam).then((docRef) => teamRefId = docRef.id);
    this.router.navigateByUrl('/dash');
}

有什么想法吗?也许那应该是某种传播运算符,但我又一次不想获取用户对象 post 它返回到 firestore 但只是更新那个字段...

您可以通过使用以下内容更新文档来执行此操作:

let teamArray = []; // if not excisting.
teamArray.push(teamRefId); // or just add it if it's.
userRef.update({teams: teamArray});

更新不会改变除团队以外的任何其他内容。如果团队不存在,将添加团队。