简体   繁体   中英

Firebase push get key with sdk 9 modular approach

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?

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM