Ошибка: приложение Firebase по умолчанию уже существует. В самом деле? - PullRequest
3 голосов
/ 01 марта 2020

Используя код здесь: https://github.com/firebase/functions-samples/blob/master/child-count/functions/index.js Я получаю сообщение об ошибке "приложение уже существует" при развертывании в Firebase. Я только изменил ссылки для работы с моим Firebase RTD.

Это легко исправить:

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

Мой вопрос: почему это происходит? Функция вызывает initializeApp() только один раз. Это потому, что приложение уже было инициализировано в других моих функциях?

'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 () более одного раза, не указав имя приложения в качестве второго аргумента».

Я не уверен в этом ...

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...