【问题标题】:Firebase Function fails connecting to Firestore using Admin SDKFirebase 功能无法使用 Admin SDK 连接到 Firestore
【发布时间】:2021-10-16 14:49:59
【问题描述】:

我正在使用 Admin SDK 编写一个 Firebase 函数,该函数应该从 Firestore 中检索数据(在 RealTime DB 插入事件时触发的函数)。

看起来 Relatime DB 运行良好,但是当我添加 Firestore 代码时,它在 Firestore 连接上失败,并在部署时出现以下错误:

    Error: Missing expected firebase config value databaseURL, config is actually{"firebase":{"projectId":"simplify-poc","databaseURL":"https://simplify-poc-default-rtdb.firebaseio.com","storageBucket":"simplify-poc.appspot.com","locationId":"us-central"}}
     If you are unit testing, please set process.env.FIREBASE_CONFIG

这是我的功能代码,我错过了什么?

const functions = require("firebase-functions");

const admin = require("firebase-admin");
admin.initializeApp();

const db = admin.firestore();

async function getHelperToken(helperId) {
  //Get token from Firestore
  const tokensRef = db.collection("tokens");
  const helper_token = await tokensRef
    .where("user", "==", helperId)
    .get("device_token");
  return helper_token;
}

//DB triggered function - upon writing a child in the HelpersInvitations reference
exports.sendHelperInvitation = functions.database
  .ref("/HelpersInvitations/{helper_invitation_id}")
  .onCreate((snapshot, context) => {
    const studentId = snapshot.val().studentId;
    const studentName = snapshot.val().studentName;
    const helperId = snapshot.val().helperId;
    const title = snapshot.val().title;
    const body = snapshot.val().body;

    //Get the helper token by Id
    const helper_token = getHelperToken(helperId);
    //Notification payload
    const payload = {
      notification: {
        title: title,
        body: body,
        icon: "default",
        click_action: "com.skillblaster.app.helperinvitationnotification",
      },
    };

  //    //Send the notification
  return admin.messaging().sendToDevice(helper_token, payload);
});

【问题讨论】:

    标签: firebase google-cloud-firestore google-cloud-functions


    【解决方案1】:

    可能是 Firebase 工具的错误。 我将 Firebase 工具从 9.16.2 升级到 9.16.5,问题消失了!

    升级:

    npm install -g firebase-tools
    

    【讨论】:

      【解决方案2】:

      像这样初始化应用程序对我有用。

      admin.initializeApp(functions.config().firebase);
      

      【讨论】:

      • 感谢您的建议。对我没有任何影响。
      • 确保您的服务帐号已在 GCP 上设置
      • 我应该明确配置 GCP 吗? FB 控制台还不够?我可以从移动客户端程序写入我的项目数据就好了。
      猜你喜欢
      • 1970-01-01
      • 2019-12-27
      • 2021-06-23
      • 2020-12-26
      • 1970-01-01
      • 2017-12-13
      • 2018-12-16
      • 2019-04-26
      • 2020-12-30
      相关资源
      最近更新 更多