Proper way to store values array-like in Firebase

前端 未结 3 1812
花落未央
花落未央 2020-12-08 22:54

I have a name of the song, with ratings I am saving for it. And it looks like pic bellow, as I am using push() to store each new rating.

相关标签:
3条回答
  • 2020-12-08 23:22

    I wanted to create a reusable function to add/save any object under a root node (even if that has an array of data at any level within the object). So I came up with this. (I am not sure if conforms to the best practices, but it worked pretty smooth)

        SaveWholeData: function(sKey, oVal, bGenKey) {
            bGenKey = bGenKey === undefined ? true: bGenKey;
            var oOriginalProperty = angular.copy(oVal);
            for (var property in oVal) {
                if (oVal.hasOwnProperty(property) && oVal[property] instanceof Array) {
                    console.log(property);
                    oVal[property] = "$|$";
                }
            }
            var sOwnRef = SaveDataByKey(sKey, oVal, bGenKey);
            for (var property in oVal) {
                if (oVal.hasOwnProperty(property) && oVal[property] === "$|$") {
                    oVal[property] = oOriginalProperty[property];
                    var updatedReference = sOwnRef + "/" + property;
                   SaveWholeData(updatedReference, oVal[property], false);
                }
            }
            return true;
        },
        SaveDataByKey: function(sKey, oVal, bGenKey) {
            if (bGenKey) {
                var newPostKey = firebase.database().ref(sKey).push().key;
                oVal.genKey = newPostKey;
                firebase.database().ref(sKey).child(newPostKey).set(oVal);
                return sKey + "/" + newPostKey;
            }else{
                firebase.database().ref(sKey).set(oVal);
                return sKey;
            }
        }
    

    So to add new user under root users, you call:

    SaveWholeData("users", oUserInfo, true);
    

    and to update existing user:

    SaveWholeData("users"+"/"+ oUserInfo.genKey, oUserInfo, true);
    
    0 讨论(0)
  • 2020-12-08 23:30

    You are currently using push to add new children, which automatically generates a name for the new node that is "guaranteed" to be unique across multiple clients.

    ref.push({ ip: userIP, stars: userStars });
    

    Firebase's push method is a great way to have multiple clients adding items to an ordered collection. If you want to accomplish the same using arrays, you'd have to synchronize the length of the array between the clients. Firebase's name generation doesn't require such overhead, so is normally the preferred of keeping an order collection across multiple clients.

    But in your case, you don't seem to want an ordered "append only" collection. It seems like you consider the IP address to be an identification of each node. In that case I would use the IP address as the basis for the node name:

    votes
      "1.1.1.1": 3
      "1.2.3.4": 5
      "1.7.4.7": 2
    

    You would accomplish this with:

    ref.child(userIP).set(userStars);
    

    If you want to subsequently read all votes into an array, you can do:

    var votes = [];
    votesRef.on('value', function(snapshot) {
        snapshot.forEach(function(vote) {
            votes.push({ ip: vote.key, stars: vote.val() });
        });
        alert('There are '+votes.length+' votes');
    })
    
    0 讨论(0)
  • 2020-12-08 23:38

    After get the values from database firebase, you can storege it in another array using forEach(), like so:

     const arrayPlayers = []
            this.db.database.ref('player').once('value')
               .then((snapshot)=> {               
                    snapshot.forEach((childSnapShot:any)=>{
                        arrayPlayers.push(childSnapShot.val())
                    })
                    //Here your array will be withut the node, like so:
                    //[3,5,1,...]
                    //Now sort array with JS
                    const sortPlayers = arrayPlayers.sort((a:any, b:any)=> b.score - a.score)
                    console.log(sortPlayers);
    
                })
    
    0 讨论(0)
提交回复
热议问题