从firebase删除项目

时间:2017-06-28 14:51:36

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

我是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>

1 个答案:

答案 0 :(得分:2)

如果你使用angularjs意味着。为什么你不能使用角火,这是一个由firebase提供的库。只需添加cdn并在

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

你的主角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")
})