初始化 GCP 数据存储区中的 firestore 对象
我有一个谷歌云项目 A,其 firestore 在数据存储模式下运行。我有一个脚本(用javascript编写,运行在谷歌云中的nodejs之上),运行时,需要从另一个GC项目B中以本机模式运行的firestore获取数据。
我已经在项目A的工作区中部署了该脚本,它能够轻松地从数据存储中获取记录,但无法从项目 B 中的 firestore 获取数据。
我在脚本中有以下代码(注意:我已将 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();
我可以看到 servAcc 确实已填充且有效,但 dataColl 为空。我遇到“initializeApp 再次重新初始化错误”,因此我初始化了 Fs 两次。第一次没有参数,第二次使用信用和一些随机应用程序名称,如下所示:
Fs.initializeApp({
credential: Fs.credential.cert(servAcc),
}, "randomAppName");
我仍然无法从 firestore 中看到任何数据据说来自项目 B,是的,项目 B 中的 firestore 有数据。
让我知道为什么谷歌之神对我皱眉。
I've a google cloud project A, with firestore running in datastore mode. 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.
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.
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):
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. 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:
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.
Let me know why google gods have frowned on me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要使用项目 ID (projectB) 初始化 Firestore 对象。对于 GCP node.js SDK,您可以在 https://cloud.google.com/nodejs/docs/reference/firestore/latest/firestore/firestore#examples 。例如
,对于 Firebase admin SDK,您可以在 https://github.com/firebase/snippets-node/blob/94a076cf3db7f2954b1d8e332a89b72b7966c034/firestore/main/index.js#L12。例如
You'll need to initialize the Firestore object with a project id (projectB). 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 . E.g.
For the Firebase admin SDK, you can see an example at https://github.com/firebase/snippets-node/blob/94a076cf3db7f2954b1d8e332a89b72b7966c034/firestore/main/index.js#L12. E.g.
我试图在同一个节点项目中初始化两个 firestore 客户端,但其他答案对我不起作用,因此发布此答案。
https://firebase.google.com/docs/projects/multiprojects#use_multiple_projects_in_your_application
主实例
辅助实例:
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.
https://firebase.google.com/docs/projects/multiprojects#use_multiple_projects_in_your_application
primary Instance
secondary Instance: