使用AngularFire $ remove方法从Firebase中删除项目

时间:2015-04-15 21:49:18

标签: javascript firebase angularfire

我的Firebase应用中有以下数据数组。我想使用AngularFire的$ remove方法删除Account3,但我无法使其工作

[MY-FIREBASE-URL]
  users
    simplelogin:1
      accounts
        0
          title: "Account1"
        1
          title: "Account2"
        2
          title: "Account3"
        3
          title: "Account4"
        4
          title: "Account5"

这是我正在使用的代码

var fbURL = [MY-FIREBASE-URL];
var ref = new Firebase(fbURL);
var authData = ref.getAuth();
if (authData) {
     console.log("User " + authData.uid + " is logged in with " + authData.provider);
     var accountPath = fbURL + "/users/" + authData.uid + "/accounts/" + account.title;
     //alert(accountPath);
     var accountRef = new Firebase(accountPath);
     accountRef.remove();
     } else {
          console.log("User is logged out");
     }

在console.log中,我在authData中看到了正确的用户但没有任何反应。我做错了什么?

2 个答案:

答案 0 :(得分:0)

试试这个。我更改了accountRef变量和您的$remove()函数:

var fbURL = [MY-FIREBASE-URL];
var ref = new Firebase(fbURL);
var authData = ref.getAuth();
if (authData) {
    console.log("User " + authData.uid + " is logged in with " + authData.provider);
    var accountPath = fbURL + "/users/" + authData.uid + "/accounts/" + account.title;
    //alert(accountPath);
    var accountRef = $firebaseObject(accountPath);
    accountRef.$remove().then(function(accountPath) {
        console.log("data has been deleted locally and in Firebase");
    }, function(error) {
        console.log("Error:", error);
    });
} else {
      console.log("User is logged out");
}

答案 1 :(得分:0)

感谢@MattDionis为您提供帮助。我终于找到了我的问题的答案,所以这里是任何其他新手,比如我在努力解决类似的问题。在上面的示例代码中,Firebase网址引用了" account.title"实际上我需要" account.id",基于上面的数据结构,实际从我的Firebase数据中删除条目。