无法检索firebase密钥并使用angular从firebase删除对象

时间:2017-09-15 19:55:52

标签: javascript angularjs firebase

我目前能够成功将数据推送到我的firebase节点(属性)并创建一个新属性,但是一旦我推送数据,我不确定如何找到创建的新属性的密钥然后删除它

我确信这很简单,但它仍然无法实现。

提前致谢。 :)

以下代码:

angular.module('propApp')
.controller('propertyCtrl', function($scope, $rootScope, $mdDialog, $mdMedia, $mdSidenav){

    let database = firebase.database();
    let ref = database.ref();
    let uid = firebase.auth().currentUser.uid;
    let propRef = ref.child('properties');

    // add new property function
    $scope.addNewProperty = function(){

        var newProp = propRef.push({
            street: $scope.property.street,
            postalCode: $scope.property.postalCode,
            apt: $scope.property.apt,
            city: $scope.property.city,
            state: $scope.property.state,
            landlord: uid,
            address: $scope.property.street + " " + $scope.property.city + " " + $scope.property.state + " " + $scope.property.postalCode + " " + $scope.property.apt
        })
    };

    // var newPropKey = newProp.push().key;


    // delete property function
    $scope.deleteProperty = function(){
        // delete property

        // new property variable
        var newPropKey = ????; //not sure if this is right

        // want to use a variable to delete property
        propRef.child('newPropKey').remove();



        console.log('deleted!');
    }

})

1 个答案:

答案 0 :(得分:-1)

var ref = firebase.database().ref('properties');

ref.push({some:'thing'}).then(function(snap) {
  console.log(snap.key);
  //look at the database to verify the key is there
  setTimeout(function() {
    snap.ref.set(null);
    //or 
    //ref.child(snap.key).set(null);
  },5000);
})