javascript - Why does angularFire $add(user) method adds the same record twice ? How to keep firebase records unique? -
i dont want angularfire keep adding duplicate records, how can make add user once , if user data has been added database not add same user again.
i have button that:
<a class="btn btn-block btn-lg btn-success" href="#" ng-hide="auth.user" ng-click="auth.$login('persona')"><span><i class="fa fa-user fa-lg"></i></span> login <strong>persona</strong></a>
and controller has:
var ref = new firebase("https://mybase.firebaseio.com/"); $scope.auth = $firebasesimplelogin(ref); var users = new firebase("https://mybase.firebaseio.com/users"); $scope.udata = $firebase(users); $scope.auth.$login('persona').then(function(user){ var usersarray = orderbypriorityfilter($scope.udata); if (_.contains((_.pluck(usersarray, 'uid')),user.uid) == false){$scope.udata.$add(user)} });
login partial , partial user gets redirected after he/she logins have separate controllers , if try add user data database in login controller (before user gets redirected) same user added 4 times in database instead of 2.
thanks in advance.
the $add
method should used create (and append) new records chronologically incremental id. if want overwrite (or create if record isn't present), can use $save or $set method.
$scope.auth.$login('persona').then(function(user){ var usersarray = orderbypriorityfilter($scope.udata); if (_.contains((_.pluck(usersarray, 'uid')),user.uid) == false){ $scope.udata.$child(user.uid).$set(user); } });
Comments
Post a Comment