I'm trying to retrieve the id of the element that I inserted in the database with .key
. However, it returns the user
value instead of the auto-generated id.
How can I get the auto-generated id ?
var mainText = document.getElementById("main-text");
var emailText = document.getElementById("emailtxt");
var prenom = document.getElementById("prenomtxt");
var submitBtn = document.getElementById("submitBtn");
var heading = document.getElementById("heading");
var firebaseHeadingref = firebase.database().ref().child("titre");
firebaseHeadingref.on('value', function(datasnapchot){
heading.innerText = datasnapchot.val();
})
function submitClick(){
var firebaseref = firebase.database().ref();
var messageText = mainText.value;
var txtmail = emailText.value;
var prenomtxt = prenom.value;
if(messageText == "" || txtmail == "" || prenomtxt == ""){
alert("Tous les champs doivent être remplis");
return;
}
firebaseref.child("user").push().set({name : messageText, prenom:prenomtxt ,email : txtmail});
var f = firebase.database().ref().child("user").orderByChild("email").equalTo(txtmail);
// firebaseref.child("user").push({name: messageText, prenom:prenomtxt, email: txtmail}).then(pushed_user => {
//
// console.log(pushed_user.key);
//
// });
f.on("value", function(datasnapchot){
var id = datasnapchot.val();
console.log(id);
});
// location.reload();
}
There is a console notice:
It looks like you're using the development build of the Firebase J5 SDK. When deploying Firebase apps to production, it is advisable to only import the individual SDK ponents you intend to use. For the CON builds, these are available in the following manner (replace ‹PACKAGE> with the name of a ponent - 1.8. auth, database, etc):
.0.0/firebase-‹PACKAGE›.js
I'm trying to retrieve the id of the element that I inserted in the database with .key
. However, it returns the user
value instead of the auto-generated id.
How can I get the auto-generated id ?
var mainText = document.getElementById("main-text");
var emailText = document.getElementById("emailtxt");
var prenom = document.getElementById("prenomtxt");
var submitBtn = document.getElementById("submitBtn");
var heading = document.getElementById("heading");
var firebaseHeadingref = firebase.database().ref().child("titre");
firebaseHeadingref.on('value', function(datasnapchot){
heading.innerText = datasnapchot.val();
})
function submitClick(){
var firebaseref = firebase.database().ref();
var messageText = mainText.value;
var txtmail = emailText.value;
var prenomtxt = prenom.value;
if(messageText == "" || txtmail == "" || prenomtxt == ""){
alert("Tous les champs doivent être remplis");
return;
}
firebaseref.child("user").push().set({name : messageText, prenom:prenomtxt ,email : txtmail});
var f = firebase.database().ref().child("user").orderByChild("email").equalTo(txtmail);
// firebaseref.child("user").push({name: messageText, prenom:prenomtxt, email: txtmail}).then(pushed_user => {
//
// console.log(pushed_user.key);
//
// });
f.on("value", function(datasnapchot){
var id = datasnapchot.val();
console.log(id);
});
// location.reload();
}
There is a console notice:
It looks like you're using the development build of the Firebase J5 SDK. When deploying Firebase apps to production, it is advisable to only import the individual SDK ponents you intend to use. For the CON builds, these are available in the following manner (replace ‹PACKAGE> with the name of a ponent - 1.8. auth, database, etc):
https://www.gstatic./firebasejs/5.0.0/firebase-‹PACKAGE›.js
Share Improve this question edited Feb 3 at 6:06 DarkBee 15.5k8 gold badges72 silver badges118 bronze badges asked Mar 21, 2019 at 16:42 MikimoucheMikimouche 511 gold badge1 silver badge5 bronze badges 1- Can you help us by adding Firebase RealtimeDatabase Data and Javascript code into the question?? – Fire-In-D-Hole Commented Mar 21, 2019 at 19:07
5 Answers
Reset to default 0Try calling it like a function:
var id = datasnapchot.key();
When you push an element you can directly retrieve the auto generated ID without having to call a listener.
firebaseref.child("user").push({name: messageText, prenom:prenomtxt, email: txtmail}).then(pushed_user => {
console.log(pushed_user.key);
});
You can find another example here.
var newPostKey = firebaseref.child("user").push().set({name : messageText,prenom:prenomtxt ,email : txtmail}).key;
"newPostKey" will be your key
Try the following:
var ref = firebase.database().ref("users");
ref.push().on("value", function(snapshot) { snapshot.forEach(function(childSnapshot) {
var id = childSnapshot.key();
console.log(childSnapshot);
});
});
You need to loop using forEach
and then you will be able to retrieve the push key using key()
This worked for me
var id = datasnapchot.key;
Use datasnapchot.key instead of datasnapchot.key()