【问题标题】:Running firebase admin and firestore on google app engine在谷歌应用引擎上运行 firebase admin 和 firestore
【发布时间】:2021-05-16 17:24:26
【问题描述】:

我目前在 Google App Engine 上运行节点 js 服务器,在服务器中,我使用 firebase-admin 包与 firebase 交互。我正在关注文档https://firebase.google.com/docs/admin/setup 并像这样初始化应用程序:

import admin from 'firebase-admin';

const NODE_ENV = process.env.NODE_ENV;

if (NODE_ENV === 'production') {
  admin.initializeApp();
} else {
  // eslint-disable-next-line @typescript-eslint/no-var-requires
  const firebaseAccountCredentials = require('../../credentials.json');
  const serviceAccount = firebaseAccountCredentials as admin.ServiceAccount;
  admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
  });
}

export const db = admin.firestore();

它在本地运行良好,因为我传递了credentials,但是当它在 App Engine 中运行时,我看到这个错误:details: 'The Cloud Firestore API is not available for Datastore Mode projects.'

我是否必须设置服务帐户才能连接到 Firebase?如果有怎么办?找不到相关文档。

这是我的app.yaml

runtime: nodejs
env: flex

谢谢

【问题讨论】:

  • 您提供的 Firebase 文档链接确实列出了设置服务帐户的信息。那里提供的代码看起来与你的略有不同
  • 你看到的错误信息和这个问题一样。 stackoverflow.com/questions/58001922/…

标签: node.js firebase google-app-engine google-cloud-platform google-cloud-firestore


【解决方案1】:

您收到的错误消息非常不言自明。发生的情况是您尝试使用 Firestore 管理 SDK 在 Datastore 模式下连接到 Firestore DB,这是不可能的。

这在您的本地环境中工作的原因可能是因为您使用的是在 Native 模式下正确设置 Firestore 的模拟器,但在实时环境中您可能将其设置为具有 Datastore 模式的 Firestore,以便修复你必须create another Firestore in Native mode

【讨论】:

  • 是的,我在 GCP 的 Native 中激活了 Firestore,现在一切正常。非常感谢:)
猜你喜欢
  • 1970-01-01
  • 2021-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多