2017-09-15 67 views
0

我目前能够成功将数据推送到我的Firebase节点(属性)并创建新属性,但是一旦我推送数据,我不确定如何找到创建的新属性的关键字,然后将其删除。无法检索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!'); 
    } 

}) 
+0

我不知道很多关于角以及如何使用这些信息,但是[火力推(https://firebase.google.com/docs /reference/js/firebase.database.Reference#push)返回对新创建记录的引用。您可以使用'newProp'对firebase进行新的操作。 – bennygenel

回答

-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); 
}) 
+0

尽管您的方法正确地获取相关密钥,但我不明白为什么需要使用setTimeout来删除数据。根据这个问题,你只需要获取密钥,以便以后可以使用它来删除。 –

+0

超时值只是为了让他/她可以在数据库控制台中看到/消失的值作为其推送/移除值,否则不清楚该值是否实际添加到了firebase中,因为海报会在添加后立即删除它。 – Vincent

+0

从我可以从问题中收集的信息,用户不希望在推送后立即删除该属性。相反,他/她需要钥匙才能以必要的方式删除或操作。 –