Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you insert a "reference" value into firestore?

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

like image 747
AskYous Avatar asked Jul 11 '18 18:07

AskYous


People also ask

How do I add a reference to Firebase?

References are lightweight, so you can create as many as you need, and they are also reusable for multiple operations. To create a reference, get an instance of the Storage service using getStorage() then call ref() with the service as an argument. This reference points to the root of your Cloud Storage bucket.

How do references work in firestore?

< T > A DocumentReference refers to a document location in a Firestore database and can be used to write, read, or listen to the location. The document at the referenced location may or may not exist. A DocumentReference can also be used to create a CollectionReference to a subcollection.

How do references work in Firebase?

A Reference represents a specific location in your Database and can be used for reading or writing data to that Database location. You can reference the root or child location in your Database by calling firebase. database(). ref() or firebase.


Video Answer


4 Answers

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)
like image 51
trojek Avatar answered Oct 18 '22 18:10

trojek


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) => { ... })
    })
like image 29
Phillip Boateng Avatar answered Oct 18 '22 18:10

Phillip Boateng


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)
           })
   .
   ..
   ...
  }
}
like image 4
uzay95 Avatar answered Oct 18 '22 19:10

uzay95


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.

like image 3
Doug Stevenson Avatar answered Oct 18 '22 20:10

Doug Stevenson