【问题标题】:Delete an item from firebase从 Firebase 中删除项目
【发布时间】:2017-12-02 00:31:16
【问题描述】:

我是 Firebase 的新手,想知道如何才能从我的用户数据库中删除一个项目,并且该项目只有在用户插入特定代码(比如 1234)后才会被删除

订单的Json示例

 {
      "address_id" : "-Kn6eXyJ9XhglrVgA3eo",
      "item_qty" : 1,
      "payment_id" : "CREDIT",
      "product_id" : "item1",
      "product_image" : "chicken_maharaja",
      "product_name" : "New Chicken Maharaja",
      "product_price" : 130,
      "status" : "Queued",
      "user_id" : "D6yOQ1ZwWwYL69z80xJ0rLTASwG2",
      "user_name" : "Bogdan Incrosnatu"
    }

显示用户当前订单的控制器

.controller('lastOrdersCtrl', function($scope,$rootScope,fireBaseData,sharedUtils) {

    $rootScope.extras = true;
    sharedUtils.showLoading();

    //Check if user already logged in
    firebase.auth().onAuthStateChanged(function (user) {
      if (user) {
        $scope.user_info = user;

        fireBaseData.refOrder()
          .orderByChild('user_id')
          .startAt($scope.user_info.uid).endAt($scope.user_info.uid)
          .once('value', function (snapshot) {
            $scope.orders = snapshot.val();
            $scope.$apply();
          });
          sharedUtils.hideLoading();
      }
    });



})

LastOrder.html

<ion-view style="" class=" " id="page10" title="Orders">
    <ion-content class="has-header" padding="true">


      <ion-list ng-repeat="item in orders" ng-hide="user.hide">

        <ion-item class="item-thumbnail-left" >

          <img  ng-src="{{'img/fk/'+ item.product_image +'.jpg'}}" >

          <a class="badge badge-balanced" style="position: absolute;right:0;" >{{item.status}}</a>
          <h4> {{item.product_name}} </h4> 

          <button ng-click="removeRecord(user)">Delete</button>



        </ion-item>

      </ion-list>


    </ion-content>
</ion-view>

【问题讨论】:

    标签: angularjs firebase ionic-framework firebase-realtime-database firebase-authentication


    【解决方案1】:

    如果您使用 angularjs 意味着。为什么不能使用 angular fire,它是 firebase 提供的库。只需添加 cdn 并在

    中注入 firebase 模块
    var app=angular.module("appname",["firebase"]) like this
    

    您的主要 Angular js 模块,然后在您的控制器中。注入 $firebaseArray(firebase 提供的服务)。

    app.controller("controllername",function($scope,$firebaseArray){
      firebase.auth().onAuthStateChanged(function (user) {
      if (user) {
        $scope.user_info = user;
    
       var fb_query= firebase.database().ref("Pass your tree name here")
          .orderByChild('user_id')
          .startAt($scope.user_info.uid).endAt($scope.user_info.uid);
    

    然后将其传递给firebasearray

    $scope.orders=$firebaseArray(fb_query);
    $scope.orders.$loaded().then(function(){
        //you will get data here.
     var Rec= $scope.orders.$getRecord(//Pass the Id here)
     //you have to pass record id i.e($id) from the firebase object, to delete a data from firebase tree.
     //for ex
     var Rec= $scope.orders.$getRecord(1);
     if(Rec){
       scope.orders.$remove(rec).then(function(){
       //Success 
    }).catch(function(){
       //error..
    })
     }
        })  
      }
    });
    })
    

    // 或做类似的事情。

    var object={}
    object["/Your tree name"+ pass ur Id]=null;
    firebase.database().ref().update(object).then(function(){
      console.log("Successfully deleted")
    })
    

    【讨论】:

    • 我已经有了代码的第一部分,但我不完全明白我应该在哪里添加第二部分,即带有 firebase 数组的部分
    • 我不明白究竟是什么以及是否应该在 cmets 所在的位置添加任何内容
    • 告诉我你不明白的地方。
    • 您好!你在吗?