I would like to register users manually in firebase. How can i check if the user is registered already or if his ( USERID ) exists? If it exists it should not let him register otherwise if his userid is not yet on the database then his info should be saved. Here is my current code wherein only saving userinfo is still available.
$scope.details={};
$scope.registerme= function() {
var someDate = new Date();
var ref = firebase.database().ref('/Users/');
$scope.submitme = $firebaseArray(ref);
$scope.submitme.$add({
facebookid: $scope.details.userid,
firstname: $scope.details.firstname,
lastname: $scope.details.lastname,
timestamp: someDate.toString(),
}).then(function(ref) {
alert('Registration success.');
}).catch(function(error) {
alert('Registration Failed.');
});
};
There is nothing built into AngularFire for detecting if a node exists. But since AngularFire is built on top of the Firebase JavaScript SDK, you can do this with the JavaScript API:
The important thing to realize is that this snippet uses a
value
event, which will firenull
if there is no data at the current location.A
$firebaseArray()
from AngularFire on the other hand uses Firebase'schild_*
events, which cannot be used to detect existence of a specific child in the collection.If you set up a link to your db entity as FirebaseObjectObservable, then you can use exists() to check if your entity exists. That how I check if my table exists and if not, I'll fill it with initial data:
vehicle.service.ts: