I'm trying to create a simple program that takes name, mobile number and email address from user and then puts the data on Firebase Realtime Database.
There are 3 input boxes and a button which on click does the above operation. Here is the code:
<input type="text" id="name">
<input type="text" id="mobile">
<input type="text" id="email">
<button type="button" id="button" onclick="addLead()">Submit</button>
I've set up Firebase like this:
<script src="https://www.gstatic.com/firebasejs/3.2.0/firebase.js"></script>
<script>
// Initialize Firebase
var config = {
...
};
firebase.initializeApp(config);
var database = firebase.database();
</script>
And here is my addLead()
function:
function addLead() {
var clientName = document.getElementById('name').value;
var clientMobile = document.getElementById('mobile').value;
var clientEmail = document.getElementById('email').value;
var newClientKey = database.ref().child('leads').push().key;
database.ref('leads/'+newClientKey+'/name').set(clientName);
database.ref('leads/'+newClientKey+'/mobile').set(clientMobile);
database.ref('leads/'+newClientKey+'/email').set(clientEmail);
}
This is how I put data to database which works.
So the newClientKey
is generating some string, it works well when inserting but now how to retrieve? The official documentation is of no help. This generated string might be based on timestamp, if that's the case then generating it again would be impossible.
When retrieving how will I get access to email, mobile and name under that uniquely generated string which was only available at the time of inserting?
Going by the above structure, I need to get 2 levels down to access the required data and because of the lack of documentation, I don't know how to do that, I'm not sure if something like this will work:
database.child().child();
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…