Javascript 在 Firebase 上:once("value").then(); 出错了
Javascript on Firebase : something wrong in once("value").then();
为了在Firebase上搭建一些云函数,我2天前开始学习Javascript,试图在"achievementsCount_total"节点下计算和存储一些数据(数据库请参考图)结构体)。我已经成功创建节点。但是,存储在该节点下的数据并不是我所期望的。为了测试发生了什么,我修改了我的代码,如下所示:
const functions = require('firebase-functions');
// The Firebase Admin SDK to access the Firebase Realtime Database.
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.updateScores = functions.database.ref('/users/{userUID}/achievements')
.onWrite(event => {
var database=admin.database();
var achievementsCount_total=new Array();//not important here
for(var i=0;i<60;i++){achievementsCount_total[i]=0;}//not important here
var allUsersRef=database.ref("/users");//a list containing all users
var hasChildren=123;
allUsersRef.once("value").then(function (snapshot)//SOMETHING WENT WRONG HERE!
{
hasChildren=snapshot.hasChildren();//either "true" or "false"
snapshot.forEach(function(element)
{
// . . . not important here
});
});
return admin.database().ref("/achievementsCount_total").set(hasChildren);
});
令我惊讶的是,节点"achievementsCount_total"下的值既不是"true"也不是"false",而是“123”!这意味着有问题:
allUsersRef.once("value").then(function (snapshot){. . .});
好像函数没有执行(所以变量"hasChildren"的值还是123),不知道为什么。有人知道答案吗?谢谢~
allUsersRef.once("value").then(function (snapshot)//SOMETHING WENT WRONG HERE!
{
hasChildren=snapshot.hasChildren();//either "true" or "false"
snapshot.forEach(function(element)
{
// . . . not important here
});
});
是一个异步函数
return admin.database().ref("/achievementsCount_total").set(hasChildren);
不等待它 运行,为了解决这个问题,你必须 return 它在 .then()
这应该有效
return allUsersRef.once("value").then(function (snapshot)//SOMETHING WENT WRONG HERE!
{
hasChildren=snapshot.hasChildren();//either "true" or "false"
snapshot.forEach(function(element)
{
// . . . not important here
});
return admin.database().ref("/achievementsCount_total").set(hasChildren);
});
为了在Firebase上搭建一些云函数,我2天前开始学习Javascript,试图在"achievementsCount_total"节点下计算和存储一些数据(数据库请参考图)结构体)。我已经成功创建节点。但是,存储在该节点下的数据并不是我所期望的。为了测试发生了什么,我修改了我的代码,如下所示:
const functions = require('firebase-functions');
// The Firebase Admin SDK to access the Firebase Realtime Database.
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.updateScores = functions.database.ref('/users/{userUID}/achievements')
.onWrite(event => {
var database=admin.database();
var achievementsCount_total=new Array();//not important here
for(var i=0;i<60;i++){achievementsCount_total[i]=0;}//not important here
var allUsersRef=database.ref("/users");//a list containing all users
var hasChildren=123;
allUsersRef.once("value").then(function (snapshot)//SOMETHING WENT WRONG HERE!
{
hasChildren=snapshot.hasChildren();//either "true" or "false"
snapshot.forEach(function(element)
{
// . . . not important here
});
});
return admin.database().ref("/achievementsCount_total").set(hasChildren);
});
令我惊讶的是,节点"achievementsCount_total"下的值既不是"true"也不是"false",而是“123”!这意味着有问题:
allUsersRef.once("value").then(function (snapshot){. . .});
好像函数没有执行(所以变量"hasChildren"的值还是123),不知道为什么。有人知道答案吗?谢谢~
allUsersRef.once("value").then(function (snapshot)//SOMETHING WENT WRONG HERE!
{
hasChildren=snapshot.hasChildren();//either "true" or "false"
snapshot.forEach(function(element)
{
// . . . not important here
});
});
是一个异步函数
return admin.database().ref("/achievementsCount_total").set(hasChildren);
不等待它 运行,为了解决这个问题,你必须 return 它在 .then()
这应该有效
return allUsersRef.once("value").then(function (snapshot)//SOMETHING WENT WRONG HERE!
{
hasChildren=snapshot.hasChildren();//either "true" or "false"
snapshot.forEach(function(element)
{
// . . . not important here
});
return admin.database().ref("/achievementsCount_total").set(hasChildren);
});