Firestore-如何使用DocumentReference指向另一个项目中的文档? [英] Firestore - How can I use DocumentReference to point a document from another project?

查看:58
本文介绍了Firestore-如何使用DocumentReference指向另一个项目中的文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知, firebase.firestore.DocumentReference< T> 可以指向当前项目中的文档.可以指向另一个项目中的文档吗?

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?

具有这种能力是合理的,因为Firebase已经将文件写到了共享如何在一个应用程序中使用多个项目.

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

推荐答案

是否可以指向另一个项目中的文档?

Is it possible to point a document from another project?

是的,有可能.以下代码显示了如何使用一个 primary 项目的Firestore DB中的值来查询 secondary 项目的Firestore DB.

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?

我想是这样,因为正如您所提到的,Firebase提供了可能性.

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

这篇关于Firestore-如何使用DocumentReference指向另一个项目中的文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆