Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
110
rated 0 times [  115] [ 5]  / answers: 1 / hits: 79557  / 8 Years ago, thu, august 4, 2016, 12:00:00

I am pushing data in firebase, but i want to store unique id in my database also .
can somebody tell me,how to push the data with unique id.



i am trying like this



  writeUserData() {
var key= ref.push().key();
var newData={
id: key,
websiteName: this.webname.value,
username: this.username.value,
password : this.password.value,
websiteLink : this.weblink.value
}
firebase.database().ref().push(newData);
}


error is ReferenceError: ref is not defined


More From » reactjs

 Answers
6

You can get the key by using the function key() of any ref object




There are two ways to invoke push in Firebase's JavaScript SDK.




  1. using push(newObject). This will generate a new push id and write the data at the location with that id.


  2. using push(). This will generate a new push id and return a reference to the location with that id. This is a pure client-side
    operation
    .




Knowing #2, you can easily get a new push id client-side with:



var newKey = ref.push().key();


You can then use this key in your multi-location update.




https://stackoverflow.com/a/36774761/2305342




If you invoke the Firebase push() method without arguments it is a
pure client-side operation.



var newRef = ref.push(); // this does *not* call the server


You can then add the key() of the new ref to your item:



var newItem = {
name: 'anauleau'
id: newRef.key()
};


And write the item to the new location:



newRef.set(newItem);



https://stackoverflow.com/a/34437786/2305342



in your case :



writeUserData() {
var myRef = firebase.database().ref().push();
var key = myRef.key();

var newData={
id: key,
Website_Name: this.web_name.value,
Username: this.username.value,
Password : this.password.value,
website_link : this.web_link.value
}

myRef.push(newData);

}

[#61145] Tuesday, August 2, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
rebekahalysah

Total Points: 304
Total Questions: 96
Total Answers: 102

Location: Taiwan
Member since Mon, May 2, 2022
2 Years ago
;