Get initial collection values from Firebase - javascript

In my node app I try to get access to Firebase, which contains a few collections.
var firebase = require('firebase');
firebase.initializeApp({myConfig: "Here"});
var database = firebase.database();
var rootRef = firebase.database().ref()
How exactly do i get all rows of a particular collection or all collections in database? Printing those variables gives strange structured objects.

You should totally be looking into firebase documentation to get this information.
The way you retrieve will depend on what exact behavior you are expecting. And the documentation is excential to understand how firebase behave as a database in wich one of the possible cases.
var rootRef = firebase.database().ref().on('value', function(snapshot) {
console.log(snapshot.val());
});
Snippet above will look into any change on your entire database (since you are not specifying any child like ref().child("users")) and log it as a javascript Object.
Good luck and, again, go to the documentation. :)

Related

how to check if value exists in realtime database firebase

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.

How to query from firebase realtime database?

How to get all the books of a specific author from my database?
Here is a snapshot of my database, i want to get "Colson Whitehead"
for web development, javascript.
To get all books by author Colson Whitehead, you do a query like this:
var query = firebase.database().ref("books").orderByChild("author").equalTo("Colson Whitehead");
query.on("value", function(snapshot) {
snapshot.forEach(function(bookSnapshot) {
console.log(bookSnapshot.key+": "+bookSnapshot.val());
});
})
This callback will get called initially and then every time something about the books by Colson Whitehead changes. If you only want the initial call, use once instead of on.

Firebase Multipath Fetch Data

Currently, Firebase supports multipath data updates for a flat data layout. However when it comes to fetching data, is it possible to use a similar method to multipath fetch data from multiple locations in Firebase?
For example I need to join some data together and my current code looks like this:
ref.child('data_1/').once('value', function (snapshot) {
var test_string_1 = snapshot.val();
ref.child('data_2/').once('value', function (snapshot) {
var test_string_2 = snapshot.val();
//...etc
});
});
When it comes to having to fetch data from multiple nodes, this nested approach goes quite deep. So I'd like to know if there is a better and faster method, like multipath fetching, to get data from multiple locations.
If the paths/refs are not dependent on each other, you can retrieve them like this:
var test_1 = ref.child('data_1/');
var test_2 = ref.child('data_2/');
Promise.all([
test_1.once('value'),
test_2.once('value')
]).then(function(snapshots) {
var test_string_1 = snapshots[0].val();
var test_string_2 = snapshots[1].val();
});
See:
The MDN documentation on Promise.all()
Speed up fetching posts for my social network app by using query instead of observing a single event repeatedly

Create a dynamic array on a node server from the firebase database that updates with child_removed

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?

How do you pass a variable limit into a Backbone.Firebase.Collection?

The firebase documentation here (https://github.com/firebase/backfire#backbonefirebasecollection) mentions that you can apply a limit to a Backbone.Firebase.Collection:
var Messages = Backbone.Firebase.Collection.extend({
firebase: new Firebase("https://<your-firebase>.firebaseio.com").limit(10)
});
I would like to make that limit a variable - so I could sometimes show 10 records from the collection and sometimes 100 (as an example).
Can anyone recommend the best way to achieve this?
Firebase now supports query limits, using limitToFirst or limitToLast
The following code would get the first 2 records.
var ref = new Firebase("https://dinosaur-facts.firebaseio.com/");
ref.orderByChild("height").limitToFirst(2).on("child_added", function(snapshot) {
console.log(snapshot.key());
});
More info in the docs: https://www.firebase.com/docs/web/api/query/limittofirst.html

Categories

Resources