【问题标题】:Nested firebase.database function not working嵌套的 firebase.database 功能不起作用
【发布时间】:2019-04-23 16:07:02
【问题描述】:

我的嵌套 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} 上的 $ 是故意的,不要认为那里有问题。

【问题讨论】:

  • 您是否尝试在内部firebase.database() 调用之前添加return?如果不这样做,函数会立即返回,因此获取数据的异步调用可能不起作用。
  • 添加了答案。如果我有时间,我会尝试用你的方法来做。

标签: javascript firebase firebase-realtime-database google-cloud-functions


【解决方案1】:

通过以下方式解决了:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-22
    • 2021-07-12
    • 2016-11-18
    • 2019-11-30
    • 1970-01-01
    • 2012-11-02
    相关资源
    最近更新 更多