0

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);
Dave Newton
  • 158,873
  • 26
  • 254
  • 302
acchang
  • 59
  • 7
  • I'm not sure I understand--you have a local `newPostKey` and a global one that you do nothing with--it's `undefined` because you are shadowing it with your local `newpostKey`. – Dave Newton Aug 01 '20 at 01:49
  • thanks -- mainly, I set the global `newPostKey` to be able to work with it globally. I guess this isn't the way to do it. Is there a better way? (btw checked out https://blog.makersend.com/ and got "Potential Security Risk Ahead" on chrome and Firefox!) – acchang Aug 01 '20 at 01:50
  • I’m saying you never touch the global value; you are declaring local variables with the same name. – Dave Newton Aug 01 '20 at 01:57
  • ah, gotcha, I guess I have to find a way of declaring it with a function so I can use it elsewhere -- like this? https://stackoverflow.com/questions/11813806/javascript-get-a-functions-variables-value-within-another-function – acchang Aug 01 '20 at 02:13
  • oof, I got it, I used a "var" and a "let" for `newPostKey` when one will do as the declaration, the second should merely be to re-define it. – acchang Aug 01 '20 at 13:47

1 Answers1

1

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);
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807