【问题标题】:Error: The default Firebase app already exists. Really?错误:默认 Firebase 应用已存在。真的吗?
【发布时间】:2020-06-14 00:10:36
【问题描述】:

在此处使用代码:https://github.com/firebase/functions-samples/blob/master/child-count/functions/index.js 我在部署到 Firebase 时收到“应用已存在”错误。我只更改了 refs 以使用我的 Firebase RTD。

很容易解决:

!admin.apps.length ? admin.initializeApp() : admin.app();

我的问题是,为什么会发生这种情况?该函数只调用一次initializeApp()。是不是因为我的其他函数中已经初始化了app?

'use strict';

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

// Keeps track of the length of the 'likes' child list in a separate property.
exports.countlikechange = functions.database.ref('/posts/{postid}/likes/{likeid}').onWrite(
    async (change) => {
      const collectionRef = change.after.ref.parent;
      const countRef = collectionRef.parent.child('likes_count');

      let increment;
      if (change.after.exists() && !change.before.exists()) {
        increment = 1;
      } else if (!change.after.exists() && change.before.exists()) {
        increment = -1;
      } else {
        return null;
      }

      // Return the promise from countRef.transaction() so our function
      // waits for this async event to complete before it exits.
      await countRef.transaction((current) => {
        return (current || 0) + increment;
      });
      console.log('Counter updated.');
      return null;
    });

// If the number of likes gets deleted, recount the number of likes
exports.recountlikes = functions.database.ref('/posts/{postid}/likes_count').onDelete(async (snap) => {
  const counterRef = snap.ref;
  const collectionRef = counterRef.parent.child('likes');

  // Return the promise from counterRef.set() so our function
  // waits for this async event to complete before it exits.
  const messagesData = await collectionRef.once('value');
  return await counterRef.set(messagesData.numChildren());
});

该错误还显示,“这意味着您多次调用 initializeApp() 而不提供应用名称作为第二个参数。”

我不太确定……

【问题讨论】:

  • 您能否分享您的 Cloud Functions 文件(即 index.jsindex.ts 文件)的全部内容?
  • @RenaudTarnec 好的,已编辑
  • 嗯.... 乍一看,您正确地初始化了管理应用程序实例。很难说...如果您尝试一个全新的 Firebase 项目会怎样?
  • @RenaudTarnec 我可能会这样做。我需要一个在欧洲工作的干净的friendlypix 实例...见github.com/firebase/friendlypix-web/issues/90

标签: node.js firebase google-cloud-functions firebase-admin


【解决方案1】:

我使用:

try
{
   firebaseAdmin.initializeApp()
}
catch(err){
   firebaseAdmin.app()
}

【讨论】:

    猜你喜欢
    • 2020-03-25
    • 1970-01-01
    • 1970-01-01
    • 2021-09-28
    • 2017-11-19
    • 2018-09-14
    • 2019-11-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多