0

I'm trying to create a like/unlike voting system for a post. First I'm using .push to send a text input to Firebase:

$('.btn').click(function(){
  var post = $('.status-box').val();
  var id = snapdata;
  ref.child("post").push({user: id, text: post});
  $('.status-box').val('');
});

For each "like" I want add a value to the post that already exist, the problem is that I don't know how to access the id .push is generating for each post.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • I've tried to answer, but a little more detail would be great. I'm not 100% sure what you mean by `I want add a value to the post that already exist.` – Dan Prince Jan 25 '15 at 20:49
  • When data(a post with text) is sent to Firebase a unique ID is generated by .push(), i want to get that unique id path for each post so that i can add a value to it late when a user click on a like button. So that I can order a list from the diffrent values in each post!(as a vote up and down system) – Alexis Bostrom Jan 25 '15 at 20:59
  • And what do you want that ID for? – Dan Prince Jan 25 '15 at 21:03
  • so i can .set a value to it when a button is clicked!(Beginner solution for a better solutions feel free to suggest!) – Alexis Bostrom Jan 25 '15 at 21:08
  • I still don't think I understand what you mean by set a value to an ID? An ID is just a string that Firebase uses to keep track of it's references. – Dan Prince Jan 25 '15 at 21:13

2 Answers2

2

As Dan already said, the push methods returns a reference to the new node. From there you can easily get the key:

var newPost = ref.child("post").push({user: id, text: post});
console.log('The new post is under key: ', newPost.key());
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
1

Calling .push will return a Firebase reference to the data you passed to it.

Return Value

Firebase
A Firebase reference for the generated location.

https://www.firebase.com/docs/web/api/firebase/push.html

So in theory you shouldn't need the generated id because you have the reference that it pointed to.

Community
  • 1
  • 1
Dan Prince
  • 29,491
  • 13
  • 89
  • 120