dimanche 26 février 2017

How can I update a Firebase element via Javascript?

I am programming a little app in which you have a score system (like 10 points a user) and I am doing this via the Firebase database.

My problem is, that I don't know how to update my database element which I have created on the beginning. I want to update my point via a Javascript script, which can be triggered.

So this is my current code:

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");
                }
            });
        }
    });

Thanks in advance!




Aucun commentaire:

Enregistrer un commentaire