Reputation: 437
I am working on a fantasy soccer web app based on a local league. So far, when a user is created using firebase auth, there is a respective node created in my firebase using the user uid as follows:
users:{
user UID: {
email:
teamname:
fullname:
week:
}
}
I have an empty array called $scope.history and a buy function for each selected player:
$scope.history = [];
$scope.buy = function(player) {
$scope.total = 50000000;
//remove if already added
var index = $scope.history.indexOf(player);
if(index>=0){
$scope.history.splice(index,1);
return;
}
//max 6 allowed
if($scope.history.length>=6){
alert('max 6 allowed');
return;
}
var selected = $scope.history.reduce(function(a,b){
a[b.position] = (a[b.position] || 0) + 1;
return a;
}, {}) || {};
if(!selected[player.position] || selected[player.position]<2){
$scope.history.push(player);
}else{
alert('You can add only two players per position');
}
};
$scope.getTotal = function(){
return $scope.history.reduce(function(tot, p){
tot = tot - p.price;
return tot;
}, $scope.total);
};
MY Issue
My issue is when the user selects the 6 player array (each player is a JSON object), how do I get that array posted to the "week" child node under the respective user's user UID parent node?
I have tried doing this but was unsuccessful:
$scope.saveTeam = function(){
var ref2 = firebase.database().ref("users/" + auth.uid + "/week");
ref2.set($scope.history);
};
Is there a way to get each user's 6 player selection to be posted to the "week" node such that each week, the selection can be changed/ updated ?
Upvotes: 1
Views: 288
Reputation: 98
Try with the update function. If you use push or set it will create new node under the week and there it will keep your array but if you use update it will store directly.
const ref = firebase.database().ref('users').child(userUID).child('week');
ref.update($scope.history);
It will store the data like this:
Upvotes: 1