如何使用 Node 连接到非默认 Firestore 数据库(使用多个 Firestore 数据库)?

Tay*_*r S 7 javascript node.js firebase firebase-admin google-cloud-firestore

我的项目中有多个 firestore 数据库。数据库是使用命令行创建的,我可以按照此处的说明在 Firestore 数据库预览中看到它:https: //cloud.google.com/blog/products/databases/manage-multiple-firestore-databases-in-a -项目

我能够连接到默认数据库,但在连接到其他命名数据库时遇到问题。我希望能够更新/删除其他数据库中的数据。

我正在尝试使用最新的 firebase-admin sdk (11.10.1) 连接到数据库,该 SDK 支持多个命名数据库( https://firebase.google.com/support/release-notes/admin/node )

我想使用函数getFirestore(databaseId)getFirestore(app, databaseId)https://firebase.google.com/docs/reference/admin/node/firebase-admin.firestore),但当我尝试保存数据时出现以下错误:

错误:3 INVALID_ARGUMENT:请求是针对数据库“projects/testproject/databases/testdb”,但试图访问数据库“projects/testproject/databases/(default)”

我的代码如下所示:

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

const app = initializeApp({
  credential: applicationDefault(),
});

const db = getFirestore();
const otherFirestore = getFirestore('testdb');

const saveData = async (col, doc, data) => {
  await otherFirestore
    .collection(col)
    .doc(doc)
    .set(data, { merge: true });
};
Run Code Online (Sandbox Code Playgroud)

如果我使用 db 而不是 otherFirestore,数据将保存到我的默认数据库中。我也尝试过这样做,const otherFirestore = getFirestore(app, 'testdb');但最终出现了同样的错误。我希望将数据保存到我的 testdb 数据库中。

任何帮助将不胜感激,谢谢!

Tay*_*r S -1

感谢您的帮助!看起来我的包没有正确更新,尽管"firebase-admin": "^11.10.1",我的 package.json 中有。必须运行以下命令,然后再次尝试似乎已经修复了它。

npm install firebase-admin@latest --save
Run Code Online (Sandbox Code Playgroud)