javascript - 嵌套的 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}
是故意的,不要认为那里有问题。
解决方案
通过这样做弄清楚了:
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());
推荐阅读
- javascript - 当我使用 js 从选择输入中选择一个值时,如何更改复选框的值
- python - Pygame 碰撞蒙版:精灵和地面
- r - 带有二进制变量的 Pivot_long
- verilog - 我的测试台有错误,无法编译这个字符(')
- android - 如何嵌入不直接相关的 Room 实体?
- python - 如何使用 Scrapy 使用 Ajax 无限滚动抓取网站
- python - 访问嵌套字典中的数据?Python
- php - WooCommerce:将自定义元添加为隐藏的订单项元以供内部使用
- javascript - 如何在 bar chart.js 的工具提示中显示多个值?
- sql - Postgres 中用于查找最后一个值的聚合