Firebase update callback to detect error and sucssess

前端 未结 3 923
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-02 03:27

How do I make this call back work? I have read the documents but I just cant figure it out for some reason?

   var ref = new Firebase(\"https://xxx.firebasei         


        
相关标签:
3条回答
  • 2021-01-02 03:46

    In React ES6 :

    var refTypes = db.ref("products").child(productId);
    
    var updates = {};
    updates[productId] = newProduct;
    
    refTypes.update(updates).then(()=>{
      console.log("Data saved successfully.");
    }).catch((error)=> {
      console.log("Data could not be saved." + error);
    });
    
    0 讨论(0)
  • 2021-01-02 03:48

    Your second call to update() raises this error in the JavaScript console:

    Uncaught Error: Firebase.update failed: First argument must be an object containing the children to replace.(…)

    The first argument to update has to be an object, so:

    blast.update({ update: "I'm writing data" }, function(error) {
      if (error) {
        alert("Data could not be saved." + error);
      } else {
        alert("Data saved successfully.");
      }
    });
    

    For reference, this is the documentation for the update() function.

    0 讨论(0)
  • 2021-01-02 03:52

    Frank's solution is perfect for your question. Another option is to use the update promise. It's particularly useful if you're doing a bunch of operations together which is often the case in Firebase.

    here's an example using a promise

    blast.update({ update: "I'm writing data" }).then(function(){
      alert("Data saved successfully.");
    }).catch(function(error) {
      alert("Data could not be saved." + error);
    });

    0 讨论(0)
提交回复
热议问题