javascript - Angularfire child_added -


so have started using angularfire since renders use of backend useless.

i trying see in registerform if email exists. @ moment there 1 emailaddress 'xxx' in firebase, not capable of adding second entry since keep getting undefined function. clarify, here code in controller:

 $scope.submitform = function (isvalid) {         if (isvalid) {             var resolved = checkemail($scope.add.email);             console.log('resolved = ' + resolved);             if (resolved) {                 console.log('i should here');                 $scope.addentry();             } else if (!resolved) {                 console.log('am getting in here ???');             }         } else {             alert('failed!')         }     };  var db = new firebase("https://radiant-fire-1289.firebaseio.com/");  function checkemail(inputemail) {     db.on('child_added', function (snapshot) {         var data = snapshot.val();         if (data.email.touppercase() === inputemail.touppercase()) {             return false;         } else {             console.log('i in checkemail');             return true;         }     }) } 

inputemail email put in html form. function correctly returns true when submitting email address 'bbb' , not equal 'xxx'. submitform function gets called form when pressing on submit button. isvalid validation of form, works correctly. boolean 'resolved' undefined. have feeling has asynchronous functions, since newbie @ javascript (and firebase) have trouble understanding doing wrong.

the console log 'i in checkemail' before logs 'resolved = ' + resolved, resolved keeps printing undefined instead of true. console prints 'am getting here ???' leads no entry being added. (i guess undefined same false compiler? )

the on function asynchronous. means callback not executed until after remote call firebase completes. checkemail function has run before result fetched, there no return value. test adding console.log right before end of checkemail, note runs before "i'm in checkemail" log.

additionally, there misunderstanding of scope here. return false/true inside of db.on callback not somehow propagated checkemail, result of checkemail undefined, if synchronous call.

to correctly, want invoke callback when result fetched:

$scope.submitform = function (isvalid) {         if (isvalid) {             var resolved = checkemail($scope.add.email, function(resolved) {                console.log('resolved = ' + resolved);                if (resolved) {                  console.log('i should here');                  $scope.addentry();                } else if (!resolved) {                  console.log('am getting in here ???');                }             });         } else {             alert('failed!')         }     };  var db = new firebase(url);  function checkemail(inputemail, callback) {     db.on('child_added', function (snapshot) {         var data = snapshot.val();         if (data.email.touppercase() === inputemail.touppercase()) {             callback(false);         } else {             console.log('i in checkemail');             callback(true);         }     }) } 

last not least, you'll want learn angular's html compiler , $q. child_added event not going fired inside angular's compile scope (since it's asynchronous) you'll need manually trigger compile using $timeout if make changes $scope.

see firebase + angular getting started guide, introduce correct way integrate, , provides lib called angularfire handles these complexities on behalf.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -