您如何插入“参考”重视火焰?

发布于 2025-01-21 17:29:04 字数 454 浏览 0 评论 0原文

我正在尝试将文档插入集合中。我希望该文档具有类型参考的属性,以插入集合中。但是,每次我插入集合时,它都会以字符串或对象形式出现。如何通过编程方式插入参考键入值?


绝对有可能在UI中进行:

”在此处输入图像描述”

I'm trying to insert a document into a collection. I want the document to have a attribute of type reference to insert into the collection. But every time I insert into the collection, it comes out as a string or an object. How can I programmatically insert a reference typed value?

enter image description here


It's definitely possible to do it in the UI:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

稀香 2025-01-28 17:29:04

最简单的解决方案可能是将参考键的值设置为doc(collection/doc_key),因为需要documentRereference

示例代码:

post = {
  content: "content...",
  title: "impressive title",
  user: db.doc('users/' + user_key),
};

db.collection('posts').add(post)

Probably the simplest solution is to set the value of a reference key to a doc(collection/doc_key) because a DocumentReference is needed.

Example code:

post = {
  content: "content...",
  title: "impressive title",
  user: db.doc('users/' + user_key),
};

db.collection('posts').add(post)
回忆躺在深渊里 2025-01-28 17:29:04

我今天试图弄清楚这一点,而我提出的解决方案是使用.doc()创建一个doc参考,

  firebase.firestore()
    .collection("applications")
    .add({
      property: firebase.firestore().doc(`/properties/${propertyId}`),
      ...
    })

这将存储a documentReference property字段上键入字段,因此在阅读数据时,您将能够访问该数据文件如此

  firebase.firestore()
    .collection("applications")
    .doc(applicationId)
    .get()
    .then((application) => {
      application.data().property.get().then((property) => { ... })
    })

I was trying to figure this out today and the solution I came to was to use the .doc() to create a doc reference

  firebase.firestore()
    .collection("applications")
    .add({
      property: firebase.firestore().doc(`/properties/${propertyId}`),
      ...
    })

This will store a DocumentReference type on the property field so when reading the data you will be able to access the document as so

  firebase.firestore()
    .collection("applications")
    .doc(applicationId)
    .get()
    .then((application) => {
      application.data().property.get().then((property) => { ... })
    })
亽野灬性zι浪 2025-01-28 17:29:04

这是存储在Firestore的模型类。

import { AngularFirestore, DocumentReference } from '@angular/fire/firestore';

export class FlightLeg {
  date: string;
  type: string;

  fromRef: DocumentReference; // AYT Airport object's KEY in Firestore
  toRef: DocumentReference;   // IST  {key:"IST", name:"Istanbul Ataturk Airport" }
}

我需要用参考值存储Flightleg对象。为了做到这一点:

export class FlightRequestComponent {

  constructor(private srvc:FlightReqService, private db: AngularFirestore) { }

  addFlightLeg() {
    const flightLeg = {
      date: this.flightDate.toLocaleString(),
      type: this.flightRevenue,
      fromRef: this.db.doc('/IATACodeList/' + this.flightFrom).ref,
      toRef: this.db.doc('/IATACodeList/' + this.flightTo).ref,
    } as FlightLeg
    .
    ..
    this.srvc.saveRequest(flightLeg);
  }

可以将对象保存的服务用引用另一个对象到firestore中:

export class FlightReqService {
   .
   ..
   ...
  saveRequest(request: FlightRequest) {
    this.db.collection(this.collRequest)
           .add(req).then(ref => {
              console.log("Saved object: ", ref)
           })
   .
   ..
   ...
  }
}

This is the model class to store in firestore.

import { AngularFirestore, DocumentReference } from '@angular/fire/firestore';

export class FlightLeg {
  date: string;
  type: string;

  fromRef: DocumentReference; // AYT Airport object's KEY in Firestore
  toRef: DocumentReference;   // IST  {key:"IST", name:"Istanbul Ataturk Airport" }
}

I need to store FlightLeg object with reference value. In order to do this:

export class FlightRequestComponent {

  constructor(private srvc:FlightReqService, private db: AngularFirestore) { }

  addFlightLeg() {
    const flightLeg = {
      date: this.flightDate.toLocaleString(),
      type: this.flightRevenue,
      fromRef: this.db.doc('/IATACodeList/' + this.flightFrom).ref,
      toRef: this.db.doc('/IATACodeList/' + this.flightTo).ref,
    } as FlightLeg
    .
    ..
    this.srvc.saveRequest(flightLeg);
  }

The service which can save the object with referenced to another object into firestore:

export class FlightReqService {
   .
   ..
   ...
  saveRequest(request: FlightRequest) {
    this.db.collection(this.collRequest)
           .add(req).then(ref => {
              console.log("Saved object: ", ref)
           })
   .
   ..
   ...
  }
}
娇纵 2025-01-28 17:29:04

该字段的值必须为类型 。看来您正在将一些其他对象放在其中,其中包含一个名为id的属性,这是一个字符串。

The value of the field must be of type DocumentReference. It looks like you're putting some other object in there that has a property called id that's a string.

多像笑话 2025-01-28 17:29:04

似乎最近可能有一个更新使上述答案现在已经过时了。有趣的是,现在的解决方案甚至更容易。他们已删除了.REF选项,但是REF现在自动获得。

因此,您可以这样做:

const member = this3.$Firestore.doc('users/' + user2.user.uid);
this3.$Firestore.collection('teams').doc(this3.teamName).set({
  name: this3.teamName,
  members: [member],
});

成员是DOC参考的位置,很简单。 (忽略这3大声笑。)

It seems there may have been a recent update that has made the above answers outdated now. Funnily enough, the solution is even easier now. They've removed the .ref option, but the ref is automatically gotten now.

So you can just do:

const member = this3.$Firestore.doc('users/' + user2.user.uid);
this3.$Firestore.collection('teams').doc(this3.teamName).set({
  name: this3.teamName,
  members: [member],
});

Where member is the doc reference, simple as that. (Ignore the this3 lol.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文