1

I have three nodes in my firebase db: users, projects and user-projects.

I want to populate a <select> with the names of the projects, and select one option based on the preferences of the user. To achive that I follow the next steps:

  1. Get the data of the authenticated user: users/uid.
  2. Get the projects associated with that user: user-projects/uid.
  3. Transform one object with keys to an array of strings.
  4. Perform one request per key and use $.when.apply($, requests) to wait all the results (to keep the order).
  5. Finally show the select with options and pre-select one of them.

My question is, really all of these steps are neccesary?

If I store the name of the projects in user-projects I can avoid the last 3 steps, but in that case, I have to update more nodes when a project changes (and I really don't want to do that).

Also I was looking for examples that use firebase with one JS framework (react.js, vue.js), but it seems that the asynchronous requests are made with other libraries, independent of those view frameworks.

I am using the next lines of code:

// Authenticated user
var user = firebase.auth().currentUser;

// General reference to the real time db
var ref = firebase.database().ref();

// Request the user data
ref.child('users/'+user.uid).once('value').then(function(snapshot) {
    var user_data = snapshot.val(); 
    // console.log(user_data);

    // Global variable to store the id of the selected project
    project_selected_key = user_data.project_selected;

    // Get associated projects
    return ref.child('user-projects/'+user.uid).once('value');
}).then(function (projectsSnapshot) {

    // projectsSnapshot.val() != project_keys
    var project_keys = [];
    projectsSnapshot.forEach(function (e) {
        project_keys.push(e.key);
    });

    // it is not possible :/
    // return ref.child('projects').children(project_keys).once('value');

    // one request per key
    var requests = [];
    var results = [];
    project_keys.forEach(function (project_key) {
        var request = ref.child('projects/'+project_key).once('value', function (snapshot) {
            results.push(snapshot.val());
        });
        requests.push(request);
    });

    // wait until all requests are complete
    $.when.apply($, requests).then(function() {
        console.log(results);
    }, function (error) {
        console.log('One request has failed');
    });

}, function (error) {
    // Something went wrong
    console.error(error);
});
adjuremods
  • 2,938
  • 2
  • 12
  • 17
JCarlosR
  • 1,598
  • 3
  • 19
  • 32
  • 1
    Dunno if I can give a full answer, but check this. Essential denormalizing your data is the normal with fb. https://firebase.googleblog.com/2013/04/denormalizing-your-data-is-normal.html – Simon C Nov 10 '16 at 03:43
  • 1
    Denormalized data is the norm with just about all NoSQL persistence strategies & solutions. – Alistair A. Israel Nov 10 '16 at 04:20
  • Ok, but the steps mentioned are necessary to access the data? I am looking for examples about how to query when I have a list of keys, but I just see examples for one particular node. – JCarlosR Nov 10 '16 at 06:22
  • Might want to checkout https://stackoverflow.com/questions/35931526/speed-up-fetching-posts-for-my-social-network-app-by-using-query-instead-of-obse – Joe Hawkins Oct 23 '17 at 22:22

0 Answers0