Home > Back-end >  Firebase push get key with sdk 9 modular approach
Firebase push get key with sdk 9 modular approach

Time:04-22

As the question asks I'm attempting to get the key after I push to a firebase db.

 push(dbRef, formState)
 .then((resp) => {
   console.log(resp);
 })
 .catch(error => {
   Alert.alert(error)
 })

The above console.log gives me the full url of the data pushed. In example:

"https://example.firebaseio.com/organization/asdfasdfasdf/members/-N08ScImBoOckVIRu-AU". I need the key only: `-N08ScImBoOckVIRu-AU`

I incorrectly attempted:

 push(dbRef, formState)
 .getKey()
 .then((resp) => {
 })
 .catch(error => {
   Alert.alert(error)
 })

This gives an error. How can I accomplish this?

CodePudding user response:

If you split the push() call from the actual writing of the data, you can get the key like this:

const newRef = push(dbRef);
console.log(newRef.key);
set(newRef, formState);
  • Related