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

Deadly 提交于 2021-02-05 09:44:59

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/59982871/firestore-how-can-i-use-documentreference-to-point-a-document-from-another-pro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!