max pleaner max pleaner - 3 months ago 18
Javascript Question

Firebase is not setting data with browser API

Here's the code I have to set some data:

firebase.database().ref(key).set(val);


which returns

Promise {[[PromiseStatus]]: "pending", [[PromiseValue]]: undefined}


It suggests that I can pass it a then/catch handler, but writing:

log = function(x) {console.log(x)
firebase.database().ref(key).set(val).then(log).catch(log)


has the exact same effect (nothing is returned/printed either way)

The wierd thing is that this intermittently works, I just can't figure out why.

Answer

.set and .update are both thenable.

function update(node,key,value){
    var ref = firebase.database().ref('/');
    var obj = {};
    obj[key] = value;
    ref.child(node).update(obj)
    .then(function() {
        console.log('Update Ran Successfully');
    });       
}