首页 > 解决方案 > 更新 Firebase 实时数据库中的现有值

问题描述

我想添加到 Firebase 中的现有值我有一个 updatePoints 函数来查找用户,如果他们已经存在我想查找现有点并添加 newPoints 值。请问我该怎么做?

updatePoints(userID, newPoints){
firebase.database().ref('/userProfile').child(userID).once('value', function(snapshot) {
  var exists = (snapshot.val() !== null);

  if (exists) {
      console.log('user ' + userID + ' exists!');

        firebase.database().ref('markets/'+userID).update({ 
        points: newPoints  // I want to look up eixsitg points and add on newPoints,

      });

    } else {
      console.log('user ' + userID + ' does not exist!');
      firebase.database().ref('/markets').child(userID).set({ 
      points: 1

      });

    }
});

标签: javascriptfirebasefirebase-realtime-database

解决方案


解决了,如果这对某人有帮助...

console.log("val",snapshot.val().points);

推荐阅读