I have an alarm app using Firebase where, when the user uses it the first time, 3 sample tasks are added to the list and saved to the Firebase. In order to prevent these sample tasks added again, my code is supposed to check the snapshot (representation of data in Firebase) and skip the $scope.data.$add part. However, it keeps adding additional 3 samples every time the page is refreshed.
Tracing the code execution in the Chrome Dev tool, the ref.once() block is skipped when the page is refreshed, and therefore leaves the snapshot undefined, which explains why the sample tasks are added again and again.
Please advise what I'm missing. Thanks!
ref.once('value', function(snapshot) {
$scope.total = snapshot.numChildren();
});
if ($scope.total == 0) {
$scope.list = [
{content: "9am", event: "Sam", bgColor: "#b58900"},
{content: "9:30am", event: "Holley", bgColor: "#cb4b16"},
{content: "10:30am", event: "Nick", bgColor: "#d33682"}
];
$scope.list.forEach(function(item){
$scope.data.$add({'content':item.content, 'event':item.event, 'bgColor':item.bgColor});
});
}