0

I’ve been figuring out how to get the key and use it but couldn’t figure it out. Tried Object.keys, nothing good was on YT for it, Get Firebase child nodes' names without getting their children too in Firebase response?, Firebase REST API working with shallow data, using .getKey to get the key and store it to then use .update after receiving it etc. I’m probably overthinking it, thanks in advance.

firebase
     .database()
     .ref("Posts")
     .push({
          Title: textToSaveTitle,
          Post: textToSave,
          uid: user.uid,
          profilePic: downloadURL,
          firstLast: fln,
          username: username,
          likes:0,
          //flair: 
          document.getElementById("dropdownMenuButton").innerHTML,
           time: time + " " + cMonth + "/" + cDay + "/" + cYear
                }).getKey()
                .then(function () {
                  console.log(key);
firebase
     .database()
     .ref("Posts")
     .on("value", function (lists) {
          lists.forEach(function (data) {
            var info = data.val();
            var keys = Object.keys(lists)
            console.log(keys)
FahadA
  • 19
  • 3
  • Just to clarify you mean after doing snapshot.val(), you want to get the key just like the individual data right? (ex. data.photoLink) – Anteos1 Jul 02 '21 at 01:30
  • It is not clear what the problem is you're having. Since you indicated having tried something, please edit your question to include the [minimum, complete/standalone code that reproduces where you got stuck](http://stackoverflow.com/help/mcve) (read the link please, as it's quite useful). – Frank van Puffelen Jul 02 '21 at 02:18

2 Answers2

0

If you want to get the keys of the snapshot, you can do so with:

firebase
     .database()
     .ref("Posts")
     .on("value", function (lists) {
          let keys = []
          lists.forEach(function (data) {
            keys.push(data.key);
          }
          console.log(keys)
     });

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Ahh, I meant more of, getting the individual key so I can save it as the post’s id, I worded it wrong. – FahadA Jul 02 '21 at 13:52
0

After setting up the info variable,make another variable and set it equal to data.key to get the key for each of them, should work

Anteos1
  • 99
  • 1
  • 10