Reputation: 378
Using AngularFire, I want to add an object to my angularFireCollection ONLY if the "name" is unique. My data is structured like so:
- ClientList
- dsk32923k <-Random unique ID created by AngularFire
- name : Brian
- birthday : 3/9/82
...
- skjdsjkl3
- name : John
- birthday : 6/3/90
...
For example, I wish to verify that "Brian" is a unique name before adding the data object to the ClientList. I want this to be accomplished inside an Angular controller. What is the "Angular way" of making that happen?
Upvotes: 2
Views: 563
Reputation: 7428
It's a bit hard to do this with angularFireCollection
without iterating over all the entries to check for duplicates. It might be easier to use angularFire
and use the name as the unique key:
function MyController($scope, angularFire) {
var url = "https://<my-firebase>.firebaseio.com/ClientList";
var promise = angularFire(url, $scope, "users", {});
promise.then(function() {
$scope.addUser = function(user) {
if ($scope.users[user.name]) {
throw new Error("User already exists!");
} else {
$scope.users[name] = user;
}
}
});
}
Hope this helps!
Upvotes: 2