2016-07-08 67 views
0

我嘗試製作在線數據庫(Firebase),以便測試和學習。 我成功地在數據庫中添加了成員​​,但我不知道我的代碼從刪除的問題在哪裏。從Firebase中移除特定用戶

這裏是我的代碼:

<!DOCTYPE html> 
<html> 
    <head> 


    </head> 
    <body> 
     <button onclick="saveData()">Save Data</button> 
     <button onclick="printData()">Print Data</button> 
     <button onclick="printData2()">Print Data2</button> 
     <button onclick="remove()">Remove</button> 
     <script src="https://cdn.firebase.com/js/client/2.4.2/firebase.js"></script> 
     <script> 
     var ref = new Firebase("https://projecttest-9aee9.firebaseio.com/web/saving-data/fireblog"); 
     var usersRef = ref.child("users"); 
     function saveData(){ 
     usersRef.set({ 
      alanisawesome: { 
      date_of_birth: "June 23, 1912", 
      full_name: "Alan Turing" 
      }, 
      gracehop: { 
      date_of_birth: "December 9, 1906", 
      full_name: "Grace Hopper" 
      } 
     }); 
     } 

     function printData(){ 

     usersRef.on("value", function(snapshot) { 
     console.log(snapshot.val()); 
     }, function (errorObject) { 
     console.log("The read failed: " + errorObject.code); 
     }); 
     } 
     function printData2(){ 

     ref.child("users/gracehop/date_of_birth").on("value", function(snapshot) { 
     console.log(snapshot.val());//"December 9, 1906" 
     }, function (errorObject) { 
     console.log("The read failed: " + errorObject.code); 
     }); 
     } 
     var ref = new Firebase("https://projecttest-9aee9.firebaseio.com/web/saving-data/fireblog/users"); 
     var usersRef= ref.child("users"); 
     function remove(){ 
      usersRef.remove({ 

      .then(function() { 
      console.log("Remove succeeded.") 
          }) 
      .catch(function(error) { 
      console.log("Remove failed: " + error.message) 
           }) 
     }); 
     } 
     </script> 
    </body> 
</html> 

我是新手,我需要你的幫助!

感謝您的關注!

回答

2

remove()的語法是錯誤的,承諾都是這樣處理的:

usersRef.remove() 
    .then(function() { 
    console.log("Remove succeeded.") 
    }) 
    .catch(function(error) { 
    console.log("Remove failed: " + error.message) 
    }); 

當這是固定不變的,你需要確保usersRef對應要刪除的內容。

如果你的用戶是通過火力地堡用戶的id鍵,例如:

"users" : { 
    "8dGTb3sxVCbll" : { 
     ... 
    }, 
    "bGIav9o7PhhIB" : { 
     ... 
    }, 
} 
要設置 userRef到這樣的事情

usersRef = firebase.database().ref(`users/${user.uid}`); 

,然後簡單地做

usersRef.remove() 

還有一種可能更簡潔的刪除用戶的方式。從the docs

var user = firebase.auth().currentUser; 

user.delete().then(function() { 
    // User deleted. 
}, function(error) { 
    // An error happened. 
}); 

我並不確切地知道你的設置,所以你可能需要嘗試這兩種解決方案。 如果您有任何疑問,請告訴我。

相關問題