Rusty Shackleford
Rusty Shackleford

Reputation: 213

Returning newly created key after .push firebase

I'm new to Firebase but I need to update a prop 60 seconds after it is pushed to Firebase. How can get the dynamic key Firebase creates after a .push() is successful?

function storeData(val){
$scope.ref = new Firebase('...')

$scope.ref.push({'va1': val, 'status': 'active' });
//after push give me back the newly created key string for that item only
}

Upvotes: 0

Views: 266

Answers (2)

rdzcn
rdzcn

Reputation: 71

Pass the key property at the end of push().

function storeData(val){
  const dataRef = new Firebase('...')
  const key = dataRef.push({'va1': val, 'status': 'active' }).key;
}  
// Before returning the key value, it pushes the data to the database. 

Upvotes: 1

Rusty Shackleford
Rusty Shackleford

Reputation: 213

I figured it out. I found this worked like a charm

$scope.ref.limitToLast(1).on("child_added", function(snapshot) {
console.log(snapshot.key());
})

Upvotes: 0

Related Questions