繁体   English   中英

Firestore - 更新 DocumentReference 类型的数组

[英]Firestore - Update an array of type DocumentReference

我将 Firestore 用作副项目的准关系数据库。 在控制台 GUI 中,我可以创建一个DocumentReference数组,然后在需要时使用该数组中的值加载子组件。 我想在我的代码中做同样的事情,但我做不到。

我努力了:

constructor(private store: AngularFirestore) {}

this.store
  .collection('owners')
  .doc(this.owner.id)
  .update({
    trucks: [
      ...this.owner.trucks, // firestore array of references
      this.store.doc(`/truck/${documentReference.id}`),
    ],
  });

但这会将值作为string存储在 Firestore 中。

有没有办法将值存储为DocumentReference的实例?

如果您想将新元素推送到 Firestore 文档中的数组而不是先获取现有值,则可以使用arrayUnion() ,如下所示:

import { arrayUnion } from "firebase/firestore";

// ...

this.store
  .collection('owners')
  .doc(this.owner.id)
  .update({
    trucks: arrayUnion(this.store.doc(`/truck/${documentReference.id}`),
  });

如果您直接在任一代码片段中传递doc()的结果,则该值应该是一个DocumentReference 你能通过在场上悬停来确认吗? 它应该显示类型。

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM