how do i grab the PUSH ID generated from firebase push method. I need to store this key in my App.js file for queries in the future. im not sure how to grab this key when the user is already logged in. I posted a pic of what im reffering to, to clarify
To get the pushid based on the email provided, then try the following:
firebase.database().ref().child("users").orderByChild("email").equalTo(yourEmail).on("value", function (snapshot) {
snapshot.forEach(function(childSnapshot) {
var randomKey=childSnapshot.key;
});
});
The snapshot is at the node users then you loop inside the keys and retrieve them using the key property
https://firebase.google.com/docs/reference/js/firebase.database.DataSnapshot#key
Related
I have created a realtime database on firebase and having no issues adding and removing data in tables etc.
I currently have it setup like this:
So my goal is to check if a given value is inside my database currently.
for example, I would like to check if 'max' is currently a username in my database.
var data = db.ref('loginInfo/');
data.on('value', function(snapshot) {
_this.users = snapshot.val()
});
That is how I get all the values, it is saved into _this.users
(How do i check if a value is inside this object, i am making a login program)
if i console.log the object, this is what I see:
image
If you want to check if a child node exists under loginInfo where the username property has a value of max, you can use the following query for that:
var ref = db.ref('loginInfo/');
var query = ref.orderByChild('username').equalTo('max');
query.once('value', function(snapshot) {
console.log(snapshot.exists());
});
I'd also recommend reading the Firebase documentation on queries, as there are many more options.
I have some data in my firebase database that I want to retrieve. I did one big push so that all instances would have a unique id. I know want to retrieve this data
I know I can do this:
var gymData = firebase.database().ref('gymData/previousWorkouts')
but when I get returned data like this:
how the hell am I suppose to access that id without knowing it?
for example a user of my app is going to search for an exercise and update it. they search by name, how do I find that id and look inside there? I don't get it :/
firebase.database().ref('gymData/exrcises').orderByChild('name').equal("barbell bench press").once('value', function(snapshot) {
snapshot.forEach(function(childSnapshot) {
var childKey = childSnapshot.key;//this is id
});
});
I am working on firebase for the first time. I am confused in querying the database. I have two objects on the database, one is the Auth and second one is the Chats. In Auths I have number of UID(user id) nodes, each of these nodes have their respective username. What I am trying to do is, I want to query that Auth object and get all the usernames which is equal to the one I take from a user through input box. In my sql it will be simple as SELECT * FROM auth WHERE username = userInputValue. I need same query like this, Below is what I have done so far.
var _firbaseDB = firebase.database(),
_firebaseAuthRef = _firbaseDB.ref("Auth/");
$("body").on("submit",".search-from",function(e){
e.preventDefault();
var ev = e.target,
_allUsers = $(ev).serializeArray()
_user = _allUsers[0].value;
_firebaseAuthRef.equalTo(_user).on("child_added",function(res){
console.log(res.val());
})
})
You were almost there. What's missing is that you need to specify which attribute you're querying on:
_firebaseAuthRef.orderByChild("n").equalTo(_user).on("child_added",function(res){
console.log(res.val());
})
More info on the Firebase Database Documentation.
My goal is is to create an array on a nodeJS server that is constantly updating based on data that is changing in the backend on the firebase realtime database.
Once the data is in an array, I need to perform a calculation with MathJS, then push this new value into a separate location in my firebase database. I am able to store everything on my node server in an array initially with child_added, perform my calculation, and push it to firebase. I am also able to update my calculation when new submissions are being added from the client into the database.
var db = firebase.database();
// Event Reference
var specificEvent = db.ref('/posts/area/eventID')
var sampleArray = [];
specificEvent.on("child_added", function(snapshot) {
var newPost = snapshot.val();
//push data to array
sampleArray.push(newPost.userSubmittedNumbers);
//calculation
console.log(math.mean(sampleArray));
My problem is that I have no way of tracking which specific item I want removed from the array when child_removed is called. I thought about using the firebase generated key with each post as a key/value pair, but I believe with MathJS, everything needs to be in just an array. Would dropping MathJS and using an alternative, or simply using just code be the only solution?
Any thoughts or suggestions would be greatly appreciated. Thanks!
How about storing the key/value pairs as you already considered, then – assuming you don't mind another library – using Underscore's pluck() to get the values into an array that you can do the maths on?
I have the following structure on my Firebase database:
I would like to search for a user by name, last name or email but as I don't have the user key in the level above I don't know how I can achieve this. I'm doing and administrator session so it wouldn't have access to the user key.
I have tried:
let usersRef = firebase.database().ref('users');
usersRef.orderByValue().on("value", function(snapshot) {
console.log(snapshot.val());
snapshot.forEach(function(data) {
console.log(data.key);
});
});
But it brings all the users on the database. Any ideas?
You can use equalTo() to find any child by value. In your case by name:
ref.child('users').orderByChild('name').equalTo('John Doe').on("value", function(snapshot) {
console.log(snapshot.val());
snapshot.forEach(function(data) {
console.log(data.key);
});
});
The purpose of orderByChild() is to define the field you want to filter/search for. equalTo() can get an string, int and boolean value.
Also can be used with auto generated keys (pushKey) too.
You can find all the documentation here
A warning to avoid unpleasant surprises: when you use orderByChild and equalTo do not forget to add an index on your data (here's the doc)
If you don't all the nods will be downloaded and filtered client side which can become very expensive if your database grows.