简体   繁体   English

在 GCP 数据存储中初始化 Firestore object

[英]initializing firestore object in a GCP datastore

I've a google cloud project A, with firestore running in datastore mode.我有一个谷歌云项目 A,firestore 在数据存储模式下运行。 I have a script (written in javascript running on top of nodejs in google cloud) which when run, need to get the data from firestore running in native mode in another GC project B.我有一个脚本(用 javascript 编写,在谷歌云中的 nodejs 之上运行),运行时需要从另一个 GC 项目 B 中以本机模式运行的 firestore 获取数据。

I've deployed the script in project A's workspace and it is able to get records from datastore easily but it is unable to get the data from firestore in project B.我已经在项目 A 的工作区中部署了脚本,它能够轻松地从数据存储中获取记录,但无法从项目 B 中的 firestore 中获取数据。

I've the following code in the script (note: I've uploaded the creds.json into the cloud workspace folder in which the script also resides):我在脚本中有以下代码(注意:我已将creds.json上传到脚本所在的云工作区文件夹中):

const Fs = require("firebase-admin");
const { Datastore } = require("@google-cloud/datastore");

const datastore = new Datastore({});
const servAcc = require(`./creds.json`);

Fs.initializeApp({
  credential: Fs.credential.cert(servAcc),
});
const someFunc = async () => {
      const db = Fs.firestore();
      const dataColl = await db.collection("dataB").get();

I'm able to see that servAcc is indeed populated and valid but the dataColl is empty.我可以看到 servAcc 确实已填充且有效,但 dataColl 为空。 I've got "initializeApp is re-initialized again error", so I initialized Fs twice..first with no arguments and the second time with creds and some random appname like so:我有“initializeApp is re-initialized again error”,所以我初始化了 Fs 两次..第一次没有 arguments,第二次使用 creds 和一些随机的 appname,如下所示:

Fs.initializeApp({
  credential: Fs.credential.cert(servAcc),
}, "randomAppName");

I'm still unable to see any data from the firestore supposedly coming from project B and yes, firestore in project B has data.我仍然无法看到来自项目 B 的 firestore 的任何数据,是的,项目 B 中的 firestore 有数据。

Let me know why google gods have frowned on me.让我知道为什么谷歌之神对我不满意。

You'll need to initialize the Firestore object with a project id (projectB).您需要使用项目 ID (projectB) 初始化 Firestore object。 For the GCP node.js SDK you can find an example of creating a Firestore object with a non-default project id at https://cloud.google.com/nodejs/docs/reference/firestore/latest/firestore/firestore#examples . For the GCP node.js SDK you can find an example of creating a Firestore object with a non-default project id at https://cloud.google.com/nodejs/docs/reference/firestore/latest/firestore/firestore#examples . Eg例如

var firestore = new Firestore({ projectId:
'projectB', keyFilename: '/path/to/keyfile.json'
});

For the Firebase admin SDK, you can see an example at https://github.com/firebase/snippets-node/blob/94a076cf3db7f2954b1d8e332a89b72b7966c034/firestore/main/index.js#L12 . For the Firebase admin SDK, you can see an example at https://github.com/firebase/snippets-node/blob/94a076cf3db7f2954b1d8e332a89b72b7966c034/firestore/main/index.js#L12 . Eg例如

const serviceAccount = require('./path/to/serviceAccountKey.json');
initializeApp({
  // The `projectId` parameter is optional and represents which project the
  // client will act on behalf of. If not supplied, it falls back to the default
  // project inferred from the environment.
  projectId: 'projectB',
  credential: cert(serviceAccount)
});

I was trying to initialize two firestore clients in the same node project and the other answers didn't work for me, hence posting this answer.我试图在同一个节点项目中初始化两个 Firestore 客户端,而其他答案对我不起作用,因此发布了这个答案。

https://firebase.google.com/docs/projects/multiprojects#use_multiple_projects_in_your_application https://firebase.google.com/docs/projects/multiprojects#use_multiple_projects_in_your_application

primary Instance主实例

const {getFirestore} = require('firebase-admin/firestore');
const { initializeApp, cert } = require('firebase-admin/app');

initializeApp({projectId: 'my-project'});
const db = getFirestore();

secondary Instance:次要实例:

const secondaryServiceAccount = require('./tests-local-sa.json');
const secondaryAppConfig = {
    credential: cert(secondaryServiceAccount),
};
const secondary = initializeApp(secondaryAppConfig, 'secondary');
const cache_db = getFirestore(secondary);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM