【问题标题】:How can I update a Firebase element via Javascript?如何通过 Javascript 更新 Firebase 元素?
【发布时间】:2026-01-19 08:10:02
【问题描述】:

我正在编写一个小应用程序,其中您有一个评分系统(例如一个用户 10 分),我正在通过 Firebase 数据库执行此操作。

我的问题是,我不知道如何更新我一开始创建的数据库元素。我想通过可以触发的 Javascript 脚本更新我的观点。

这是我当前的代码:

var rootRef = firebase.database().ref();

var users = rootRef.child("users");

var log = document.getElementById('log');

    document.getElementById('registerBtn').addEventListener('click', function(event) {

        var empt1 = document.forms["form1"]["email"].value;
        var empt2 = document.forms["form1"]["password"].value;
        var empt3 = document.forms["form1"]["email"].value;

        if(empt1 == "" || empt2 == "" || empt3 == "" || !document.getElementById("txtemail").checkValidity()){
            alert("Something went wrong!");
        }
        else{
            $("#registerProgress").show();
            $("#registerBtn").hide();
            //Get email and pass
            const email = txtemail.value;
            const pass = txtpassword.value;
            const username = txtusername.value;
            const auth = firebase.auth();

            const promise = auth.createUserWithEmailAndPassword(email, pass);
            promise.catch(e => console.log(e.message));
            //users.child(username).set({ name: username });
            users.once('value', function(snapshot) {
                if (!snapshot.hasChild(username)) {
                    users.child(username).set({username: username, points: "100"});
                }
                else {
                    alert("That username is already registered");
                }
            });
        }
    });

提前致谢!

【问题讨论】:

    标签: javascript html firebase firebase-realtime-database


    【解决方案1】:

    我想您是在问如何仅在用户不存在的情况下更新 firebase 中的用户记录?假设 firebase 3.x.x:

    var usersRef = firebase.database().ref().child('users');
    
    usersRef.child(username).once('value')
      .then(function (userRecord) {
        if(userRecord.exists()) {
          alert('That username is already registered');
        } else {
          //--> There's a ref in each snapshot
          userRecord.ref.child('username').set(username)
            .then(function () {
              userRecord.ref.child('points').set(100);
            });
        }
      });
    

    加分:

    usersRef.child(username).child('points').transaction(function (points) {
      return (points || 0) + 10;
    });
    

    Official Reference mentioning transactions

    注意:不要忘记保存数字(100)而不是字符串(“100”),否则增量器将无法正常工作。祝你好运。

    【讨论】:

    • 但是每次喜欢的用户赢得关卡,他应该得到10分-->这应该在数据库中更新
    • 更新为这样做。
    • 一切正常,但我无法更新我的观点,我收到此错误消息 Uncaught ReferenceError: username is not defined 这是我编辑的代码:document.getElementById('addPoints').addEventListener('click', function(event) { var usersRef = firebase.database().ref().child('users'); usersRef.child(username).child('points').transaction(function (points) { return (points || 0) + 10; }); });
    • 那来自您的 const 用户名分配。如果常量为空,则会引发错误。如果您正在为 Web 使用编写代码,您可能希望坚持使用 ES5 Javascript 或使用 ES6 的一些帮助程序。非常好ES2015 Style GuideES5 Style Guide
    最近更新 更多