query.once("value") .then(function(snapshot) {
var childData = snapshot.val().user_name;
var childData2 = snapshot.val().user_id;
document.writeln(childData);
document.writeln("\n");
document.writeln(childData2);
});
this is the code i used for retrieving firebase database value using parent value
what changes should i make in this code for retrieving the parent values using the child value
I would like to retrieve user_name with help of password.
And also i want it to be work for other records in the users.
If the password is "2110" you can retrieve the user with:
var passwordToLookFor = "2110";
var ref = firebase.database().ref("users");
var query = ref.orderByChild("password").equalTo(passwordToLookFor);
query.once(function(snapshot) {
snapshot.forEach(function(child) { // loop over the results
console.log(child.key);
console.log(child.val().user_name);
});
});
As Doug commented, please take a moment to read the Firebase documentation on ordering and querying, as it will save you many questions going forward.
Related
I need to get specific data from firebase. My database look like this:
and I can get numara value correctly but my other keys which are E-mail and Kullanıcı Adı I can't get it with javascript because of - character and space. How can I solve it? I really need help.
Here my sample code:
rootRef.on("value", function(snapshot) {
var user = snapshot.val();
console.log(user.numara, user."E-mail");
snapshot.forEach(function(childSnapshot) {
var childData = childSnapshot.val();
//console.log(childData);
});
});
Here console.log(user) output.
If the problem is purely the - in the field name, you can use [] notation to get that specific field. Without Firebase that'd look like this:
var user = { "numara": "0536...", "E-mail": "ekslg..." };
console.log(user.numara, user["E-mail"]);
It's hard to tell, because you don't provide a lot of information, but I noticed in your code, you're using user."E-mail" but your screen capture of the firebase UI actually shows the key as email
Could it be that ?
I've spent a few days researching for the solution. I have found many answers that were quite helpful, but still I haven't been able to find a solution that fully resolved my problem. Basically all I want to do is to get current user's data (stored in firebase realtime database) and display them on the user's screen.
So far, I can display data for all the user. But how can I have it display data for only the current user? (How can I get the push key of only the current user?)
I'm not an experienced programmer (less than a year). Please use a language that is easy to understand.
I've gone through all the firebase documentation. Please don't just give me the copy of the documentation or the link to it.
Screenshot: Firebase realtime database
Screenshot: JavaScript function for retrieving current user's data
Additional JavaScript screenshot
User data is wherever you choose to store it--other than name, email, and a few other fields, the User object cannot actually store any other info. Therefore, you will need to decide where in the database you want to keep your extra data.
Assuming you choose to create a collection users with children named by uid, and you store the users' countries in those documents, the following will get you a specific user's data and you can use whatever fields you'd like from there:
let ref = database.ref('/users/' + currentUser.uid).once('value').then(function(snapshot) {
let userData = snapshot.val();
console.log(userData.country);
}
Here you go!
function showUser(){
var usref=firebase.database().ref("countries/"+uid);
//uid here is the unique code u gave to the specif user
usref.once('value', function(snapshot) {snapshot.forEach(function(childSnapshot) {
if((childSnapshot.key)=="country"){
var country = childSnapshot.val();
//use this variable here enter code here
window.alert(country);//just for checking!!!
}
if((childSnapshot.key)=="age"){
var name = childSnapshot.val();
//use this variable here
window.alert(name);//just for checking!!!
}
});
});
}
I hope this code works for you.
I have ~ 1 million users in my users "table" on Firebase.
I need to fetch the username of each user.
once('value' is a way too big of a call, so I try using on 'child_added' but without any success, I am using this code:
usersRef.on('child_added', function (snapshot) {
var user = snapshot.val();
console.log(user);
var username = user.username ? user.username : null;
// Put username in the .txt file, one username per line
});
I waited as much as 15 minute, and nothing happened. It seems to be impossible right now. What could I try besides child_added?
Retrieving one million nodes is very unlikely to ever work. You're simply trying to pull too much data over the network. In that respect there is no difference between value and child_added: both retrieve the same data over the network in the same way.
Limit the number of children you retrieve by a query, either by a query:
var ref = firebase.database().ref("users");
ref.orderByChild("username").equalTo("Dan P.").on("child_added", ...
or by directly accessing the user's node:
var ref = firebase.database().ref("users");
var user = firebase.auth().currentUser;
ref.child(user.uid).on("child_added", ...
I have a real time database with firebase and i'm using the following code to connect to the database and get some data from it.
window.onload = function(){
var databaseWebsites = firebase.database().ref('/websites').orderByChild('votes');
console.log(databaseWebsites);
databaseWebsites.on('value', function(snapshot) {
snapshot.forEach(function(childSnapshot) {
var webTemp = document.getElementById(childSnapshot.val().id);
webTemp.style.order = order;
var webText = webTemp.getElementsByClassName('likeText');
webText[0].innerHTML = childSnapshot.val().votes;
order--;
});
order = 0;
});
It gets all the data, in order and uses it correctly.
The problem is, I don't want the data on the front end to update until the user refreshes the page. The system is a voting system that is ordered by votes value, if it was constantly updating it would be a bad user experience.
Any help will be appreciated.
Thanks
Change the on to once, Firebase on listens for changes in your database node and sends a response.
databaseWebsites.on('value', function(snapshot) {
to
databaseWebsites.once('value', function(snapshot) {
An excerpt from Firebase doc
The value event is called every time data is changed at the specified
database reference, including changes to children. To limit the size
of your snapshots, attach only at the lowest level needed for watching
changes.
Visit this url to read more
The accepted response is not correct (maybe outdated?) because once() requires to add a then()
It's actually
databaseWebsites.once('value').then(function(snapshot) {}
that replaces
databaseWebsites.on('value', function(snapshot) {}
been reading docs and other posts but I am unable to remove a node. I am attempting to select the node by a value.
var eventContactsRef = firebase.database().ref('events-contacts');
eventContactsRef.orderByChild('eventContactId').equalTo(eventContactId);
then call the remove method on the result
eventContactsRef.remove(function (error) {
console.log(error);
});
nothing happens except a null error value. I am using the latest firebase, most examples are for older versions so I am unsure if I need to get the key and then attempt to delete with that as a reference?
This is the first time using firebase so I am not sure if I have saved the data correctly. here is code to save.
var key = firebase.database().ref().child('event-contacts').push().key;
var url = firebase.database().ref('/event-contacts/' + key);
url.set(eventContacts);
and screenshot
You cannot remove a query itself. You can only remove the results that match a query.
var eventContactsRef = firebase.database().ref('events-contacts');
var query = eventContactsRef.orderByChild('eventContactId').equalTo(eventContactId);
query.on('child_added', function(snapshot) {
snapshot.ref.remove();
})