Alex
Alex

Reputation: 1644

Firestore - How can I use DocumentReference to point a document from another project?

As far as I know firebase.firestore.DocumentReference<T> can point to a document within the current project. Is it possible to point a document from another project?

It is reasonable to have such ability because Firebase already has written a document to share how to use multiple projects within an app.

Upvotes: 1

Views: 422

Answers (1)

Renaud Tarnec
Renaud Tarnec

Reputation: 83163

Is it possible to point a document from another project?

Yes it is possible. The following code shows how to use a value from the Firestore DB of one primary project in order to query the Firestore DB of a secondary project.

  var primaryAppConfig = {
    apiKey: 'xxxx',
    authDomain: 'xxxx',
    projectId: 'xxxx'
  };

  var secondaryAppConfig = {
    apiKey: 'xxxx',
    authDomain: 'xxxx',
    projectId: 'xxxx'
  };

  // Initialize  primary app
  var primary = firebase.initializeApp(primaryAppConfig, 'primary');

  // Initialize a secondary app with a different config
  var secondary = firebase.initializeApp(secondaryAppConfig, 'secondary');

  var db1 = primary.firestore();
  var db2 = secondary.firestore();

  const db2DocRef = db2.collection('col1').doc('doc1');

  let getDoc = db2DocRef
    .get()
    .then(doc => {
      const valToUse = doc.data().value;

      const db1DocRef = db1.collection('col1').doc(valToUse);
      return db1DocRef.get();
    })
    .then(doc => {
      console.log(doc.data());
    });

It is reasonable to have such ability?

I guess it is, since, as you have mentioned, Firebase gives the possibility.

Upvotes: 6

Related Questions