How do you insert a “reference” value into firestore?

后端 未结 5 1211
无人及你
无人及你 2020-12-24 01:50

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 in

相关标签:
5条回答
  • 2020-12-24 02:26

    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)
               })
       .
       ..
       ...
      }
    }
    
    0 讨论(0)
  • 2020-12-24 02:39

    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)
    
    0 讨论(0)
  • 2020-12-24 02:41

    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.)

    0 讨论(0)
  • 2020-12-24 02:45

    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.

    0 讨论(0)
  • 2020-12-24 02:50

    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) => { ... })
        })
    
    0 讨论(0)
提交回复
热议问题