Vinícius Biavatti Vinícius Biavatti - 7 months ago 45
AngularJS Question

angularjs two request flow

i am having a problem with my angularjs code. I made a code to login, but I need to verify if the current user already exists in database by email. I thought to make two requests to database. (Two $http). One to get the number of user with this email in database to validate if this is different of zero, and other to insert in database if the number of users with the email is zero, but i am having a problem. Look my code please:

I made a service to verify the user email in database

this code runs, but the calls of alerts is wrong. The alert("test2") is called first then alert("test1"), but i am verifing first.

Sorry, but I am learning angular just now. This is my question! Thanks!


You'll need to move your second server request inside of your .then() handler. The current code is simply executing both server requests almost in parallel because of their async nature.

What you're looking for is something closer to this...

   if(usuario.existe == true){
      //existing code
   } else {
     //make function call to insert user