【问题标题】:How do you insert a "reference" value into firestore?如何将“参考”值插入 Firestore?
【发布时间】:2018-12-19 21:38:56
【问题描述】:

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


在 UI 中绝对可以做到:

【问题讨论】:

    标签: javascript firebase google-cloud-firestore angularfire2


    【解决方案1】:

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

    示例代码:

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

    【讨论】:

    • 很棒的提示。直到我意识到 Firestore 识别出引用并自动应用它,我才明白这个答案。我原以为这个建议是在说要复制数据,我离题了!
    • 很好的答案,感谢您让我走上正轨。
    • 它只有在我从db.doc('users/' + user_key).ref 中删除.ref 后才有效,因为db.doc() 本身返回一个DocumentReference
    【解决方案2】:

    我今天试图解决这个问题,我得到的解决方案是使用.doc() 创建文档参考

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

    这将在property 字段中存储DocumentReference 类型,因此在读取数据时您将能够访问文档

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

    【讨论】:

    • 该属性字段是模型中的 DocumentReference 还是 String?
    • 这似乎对我不起作用。我仍然收到错误ERROR FirebaseError: Function addDoc() called with invalid data,如果我只是设置它存储的字符串,这意味着这个对象/代码行会导致这个错误。我尝试添加 .ref 将其转换为 DocumentReference,但现在它似乎根本没有执行。
    【解决方案3】:

    这是要存储在 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)
               })
       .
       ..
       ...
      }
    }
    

    【讨论】:

    • ".ref" 为我工作
    【解决方案4】:

    字段的值必须是DocumentReference 类型。看起来您要在其中放置一些其他对象,该对象具有名为 id 的属性,这是一个字符串。

    【讨论】:

    • 我必须实现所有这些字段和功能吗?
    • 使用 Firestore JavaScript SDK 时,您自然会使用这些对象。无论 AngularFire 做什么,都必须包装该对象。您必须掌握底层的 DocumentReference。
    • 谢谢!我只是将值设置为从查询返回的 DocumentReference:i.imgur.com/MIY5HFU.png
    • @AskYous :这是一个有用的图像。它应该进入问题。 (文字不是图片)
    • @DougStevenson,我现在需要对reference 类型的字段执行where 查询。那可能吗?如果它很复杂,我可以提出另一个 stackoverflow 问题。
    【解决方案5】:

    似乎最近的更新使上述答案现在已过时。有趣的是,现在解决方案变得更加容易。他们删除了 .ref 选项,但现在自动获取了 ref。

    所以你可以这样做:

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

    member 是文档引用,就这么简单。 (忽略this3 lol。)

    【讨论】:

      猜你喜欢
      • 2021-05-21
      • 2021-01-18
      • 1970-01-01
      • 2022-07-01
      • 1970-01-01
      • 2018-08-12
      • 1970-01-01
      • 2021-04-23
      • 2021-06-09
      相关资源
      最近更新 更多