Issue
How to add custom key for firebase realtime database? I'm using react native fetch api to connect with database.
fetch('https://chld-43c34.firebaseio.com/players.json?auth='+token,{
method: 'POST',
body: JSON.stringify( playerData,)
}
I have user id in a variable 'userId',want to add that as the key.
Solution
When you use method: 'POST'
the database generates a unique child key for you. To instead write the data directly to the location you specify, use method: 'PUT'
and pass the exact location in the URL:
let playerid = 'your_player_id';
fetch(`https://chld-43c34.firebaseio.com/players/${playerid}.json?auth=${token}`,{
method: 'PUT',
body: JSON.stringify( playerData,)
}
Also see:
- Saving data to Firebase with REST
- C# Firebase Restful Post generate unique key
- Edit property of a child using REST Api
Answered By - Frank van Puffelen
Answer Checked By - - Gilberto Lyons (ReactFix Admin)