如何增加Firebase中现有对象的值? [英] How do I increment a value for an existing object in Firebase?

查看:45
本文介绍了如何增加Firebase中现有对象的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个计步器应用程序.我有一个iOS应用,可将每天的总和推送到/users/{mobile}/steps/{date}/当更新或添加新的步骤子级时,我要对该特定用户的所有步骤的值求和,并更新其 stepsTotal .

I'm building a step counter app. I got an iOS app that pushes the sum of each day to /users/{mobile}/steps/{date}/ When a new steps child is updated or added, I want to sum the value of all the steps for that particular user and update his stepsTotal.

要实现这一目标

  1. 找到原始用户并总结所有步骤.
  2. 将新值保存到stepsTotal.

如果有人可以在这里提供帮助,我将不胜感激.:-)

I would be most grateful if someone could give some help here. :-)

数据库

{

  "users": {
    "92291000": {
      "firstName": "Tore",
      "stepsTotal": "1500",
      "steps": {
        "02-09-2017": "500",
        "03-09-2017": "1000"
      },

import.js

var db       = admin.database();
var dbRoot   = db.ref("/");
var usersRef = dbRoot.child("users");

// This works    
function saveUser(attributes) {
  let mobile =  attributes.mobile;
  delete attributes['mobile']
  let user = usersRef.child(mobile);
  user.update(attributes);
}


function increaseSteps( { mobile=null, steps=null } = {}) {
  // Find the User
  console.log("looking for mobile", mobile);  // OK
  let userRef = usersRef.child(mobile);

  // Here I'm not able to read the old data from the user.
  userRef.transaction(function(user) {
    console.log("user: ", user);  // null
    // ^ User is null. 
  });


/*  
  If I mangage to find user above, I expect to do something like this. 
  Or it is possible to only update *stepsTotal*?
*/

  let attributes = {
    firstName: user.firstName,
    lastName: user.lastName,
    stepsTotal: user.stepsTotal + steps,
  }
  user.update( attributes );
}

推荐答案

如果我理解正确,则在此代码段中您有问题:

If I understand correctly, you have a problem in this snippet of the code:

let userRef = usersRef.child(mobile);

// Here I'm not able to read the old data from the user.
userRef.transaction(function(user) {
  console.log("user: ", user);  // null
  // ^ User is null. 
});

在Firebase数据库事务中,初始值通常为 null .从有关交易的Firebase文档:

In Firebase Database transactions the initial value is often null. From the Firebase documentation on transactions:

交易函数被多次调用

Transaction Function is Called Multiple Times

您的事务处理程序被多次调用,并且必须能够处理 null 数据.即使数据库中已有数据,运行事务功能时也可能不会在本地缓存该数据.

Your transaction handler is called multiple times and must be able to handle null data. Even if there is existing data in your database it may not be locally cached when the transaction function is run.

这是由于Firebase事务在后台工作的缘故.要了解更多信息,请在此处查看我的答案 Transition updateFunction参数为空 Firebase runTransaction无法正常运行.

This is due to how Firebase transactions work behind the scenes. To learn more about that, see my answers here Transcation updateFunction parameter is null and Firebase runTransaction not working.

解决方案是处理两种情况:如果用户节点不存在,则计算初始步数,否则更新步数:

The solution is to handle both cases: if the user node doesn't exist yet count the initial number of steps, otherwise update the number of steps:

let userRef = usersRef.child(mobile);

userRef.transaction(function(user) {
  return (user || 0) + new_steps_for_user;
});

这篇关于如何增加Firebase中现有对象的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆