首页 > 解决方案 > 嵌套的 firebase.database 功能不起作用

问题描述

我的嵌套 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 的进一步尝试也没有奏效。

no $on{teamName}是故意的,不要认为那里有问题。

标签: javascriptfirebasefirebase-realtime-databasegoogle-cloud-functions

解决方案


通过这样做弄清楚了:

    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());

推荐阅读