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
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.
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.
I want to get json data from multiple url's and display it on frontend.
Following are the url's:
1) localhost:3000/api/getdata1
2) localhost:3000/api/getdata2
3) localhost:3000/api/getdata3
Instead of using .fetch() on each of the url's like below:
.fetch('localhost:3000/api/getdata1')
.fetch('localhost:3000/api/getdata2')
.fetch('localhost:3000/api/getdata3')
Can this be done in more efficent way in ReactJs ?
I was trying:
const dataurls = [
'localhost:3000/api/getdata1',
'localhost:3000/api/getdata2',
'localhost:3000/api/getdata3'
];
const promisedurl = dataurls.map(httpGet);
Promise.all(promisedurls)
.then(data=> {
for (const d of data) {
console.log(d);
}
})
.catch(reason => {
// Receives first rejection among the Promises
});
Please suggest which one should be used or is there any efficient way to do get data from multiple url's.
ReactJS is a View layer library. It has nothing to do with how you aquire any data from server.
Even state libraries, like Redux and Reflux do not implement any method of fetching data. In most cases you do that in your custom app code. Sometimes using extra libraries (e.g. Redux middlewares).
So, yes: your Promise.all(<several https requests here>) is the most natural way to achieve that.
I'm importing data from firebase to big query which is working fine at onWrite event and used table.insert function.
Now I want to update data at big query against onUpdate event but table.update function is not available and not working.suggest some other way.
below is my code
exports.updatetobigquery =
functions.database.ref('/mn_users/{userId}/').onUpdate(event => {
const dataset =
bigquery.dataset('KHUSHUApp');//functions.config().bigquery.datasetname);
const table =
dataset.table('mn_users');//functions.config().bigquery.tablename);
console.log('Uppercasing', event.data.val());
return table.update({
'id': event.data.key,
'name': event.data.val().name,
'email': event.data.val().email
});
});
Best way to handle this:
BigQuery has the ability to UPDATE data, but BigQuery is an analytical database - not a database optimized for updates.
So avoid updating data in BigQuery, if you have other means of achieving your goals.
Instead of updates, send new rows to BigQuery - de-duplicate and merge latest values later when analyzing. This pattern is great when you need the ability to go back to each time the state changed, and analyze that.
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. :)