Вложенная функция firebase.database не работает - PullRequest
1 голос
/ 23 апреля 2019

Мой вложенный запрос firebase.database возвращает:

[DEFAULT]: Firebase: No Firebase App '[DEFAULT]' has been created - call Firebase App.initializeApp() (app/no-app).
    at error (/user_code/node_modules/firebase/node_modules/@firebase/app/dist/index.node.cjs.js:40:21)
    at app (/user_code/node_modules/firebase/node_modules/@firebase/app/dist/index.node.cjs.js:297:13)
    at Object.serviceNamespace [as database] (/user_code/node_modules/firebase/node_modules/@firebase/app/dist/index.node.cjs.js:355:47)
    at exports.watchUpdates.functions.database.ref.onCreate (/user_code/index.js:18:14)
    at cloudFunctionNewSignature (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:120:23)
    at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:151:20)
    at /var/tmp/worker/worker.js:827:24
    at process._tickDomainCallback (internal/process/next_tick.js:135:7)

Код:

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

exports.watchUpdates = functions.database
  .ref('/logs/{teamName}/session/logArchive/{id}')
  .onCreate((snapshot, context) => {

    //console.log(snapshot.val())

    firebase.database()
      .ref('/logs/{teamName}/session/statistic')
      .once('value')
      .then((statisticSnapshot) => {
      console.log(statisticSnapshot.val());
      if (statisticSnapshot.exists()) {
        let newLog = snapshot.val();
        let statistic = statisticSnapshot.val();
        let totalParticipators = statistic.totalParticipation;
        let averageEnergy = statistic.averageEnergy * totalParticipators;
        let averageEnjoyment = statistic.averageEnjoyment * totalParticipators;
        let newParticipator = totalParticipators + 1;
        let newAverageEnergy = (averageEnergy + newLog.energy) / newParticipator;
        let newAverageEnjoyment = (averageEnjoyment + newLog.enjoyment) / newParticipator;
        let newAverage = (newAverageEnergy + newAverageEnjoyment) / 2;
        let statisticTotalMembers = statistic.totalMembers;
        let participationRate = (newParticipator * 100) / statisticTotalMembers;
        let newStatisticObject = {
          totalMembers: statisticTotalMembers,
          participationRate: participationRate,
          averageEnergy: newAverageEnergy,
          averageEnjoyment: newAverageEnjoyment,
          averageStat: newAverage,
          totalParticipation: newParticipator,
        };
        return firebase.database()
          .ref('/logs/{teamName}/session/statistic')
          .set(newStatisticObject)
      }
    })
  });

Закомментированный //console.log(snapshot.val(); работает так, как я хочу, но пытается углубиться в результатыв ошибках.

firebase.admin.database() - не удалось решить проблему, и дальнейшие попытки с помощью .admin также не сработали.

нет $ на {teamName} намеренно не думаю, что естьпроблема там.

1 Ответ

0 голосов
/ 24 апреля 2019

Разобрался, выполнив:

    snapshot.ref.parent.parent.child('statistic')
      .once('value')
      .then((statisticSnapshot) => {
      console.log(statisticSnapshot.val())
});

вместо:

    firebase.database()
      .ref('/logs/{teamName}/session/statistic')
      .once('value')
      .then((statisticSnapshot) => {
      console.log(statisticSnapshot.val());
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...