I’ve been working on a simple card catalog project that takes input from a form and displays it on a card.
On each card is the option to remove the card completely, or check/uncheck a box. In order to do this, I need to access the object in the firebase real-time database.
Each object is created by a .push()
and generates a random key name, and I would like to access this key name to make changes in the object or remove it.
I read up on the documentation at https://firebase.google.com/docs/database/web/read-and-write, and it provides a way to get the key before pushing. This worked in the example provided, which used update()
, but when I tried it with my push()
, the keys did not match up.
Also, since I will need to use the key in a separate function that renders the card, I tried to make it a global variable and that returned undefined
.
Can you tell me how I can get the key for use in another function?
Thanks!
When I console.log newPostKey
inside the function here it matches what’s in the database, but when I do it outside I get a undefined
.
var database = firebase.database(); let newPostKey; function writeNewPost(uid, username, picture, title, body) { // A post entry. var postData = { author: username, uid: uid, body: body, title: title, starCount: 0, authorPic: picture }; // Get a key for a new Post. var newPostKey = firebase.database().ref().child('posts').push().key; console.log(newPostKey); // Write the new post's data simultaneously in the posts list and the user's post list. var updates = {}; updates['/posts/' + newPostKey] = postData; return firebase.database().ref().update(updates); } writeNewPost("zzz", "drew", "bucolic", "bobross", "beardo"); console.log(newPostKey);
This returns a newPostKey
that doesn’t match what I see in Firebase. Outside it’s also undefined.
function writeNewPost(uid, username, picture, title, body) { var postData = { author: username, uid: uid, body: body, title: title, starCount: 0, authorPic: picture }; var newPostKey = firebase.database().ref().child('posts').push().key; console.log(newPostKey); return firebase.database().ref().child('posts').push(postData); }; writeNewPost("zzz", "drew", "bucolic", "bobross", "beardo"); console.log(newPostKey);
Advertisement
Answer
Each time you call push
on a reference, it generates a new key. Since you call push()
twice in the second snippet, you are generating two keys.
More likely, you’re looking to do this:
var newPostKey; function writeNewPost(uid, username, picture, title, body) { var postData = { author: username, uid: uid, body: body, title: title, starCount: 0, authorPic: picture }; newPostKey = firebase.database().ref().child('posts').push().key; console.log(newPostKey); return firebase.database().ref().child('posts').child(newPostKey).set(postData); }; writeNewPost("zzz", "drew", "bucolic", "bobross", "beardo"); console.log(newPostKey);
So by using .child(newPostKey).set(postData)
instead of push(postData)
the data is added to the newPostKey
child, instead of a new key.
Since you can also get the key from the DatabaseReference
that is returned by push, that snippet can also be written as:
function writeNewPost(uid, username, picture, title, body) { return firebase.database().ref().child('posts').push({ author: username, uid: uid, body: body, title: title, starCount: 0, authorPic: picture }); }; let ref = writeNewPost("zzz", "drew", "bucolic", "bobross", "beardo"); console.log(ref.key);