How to wait till the response comes from the ShepHertz database request, in angularjs?

0 votes
I want unread chat count,So first i am getting user All friends then i am checking each sender friend unread message, then pass to tab icon ,


//Login
              $scope.login=function(){
              var validationSuccess=validateWaveForm();
              if(!validationSuccess)
              return;

              $ionicLoading.show({template: 'Please wait...'});
              var userService  = new App42User();
              userService.authenticate($scope.data.username, $scope.data.password,{
                                       success: function(object)

                                       {
                                       console.log("object is"+object);
                                       var userObj = JSON.parse(object);
                                       var result = userObj.app42.response.success;
                                       if(result){
                                       var loginUser =userObj.app42.response.users.user;
                                       console.log(loginUser)
                                       localStorage.setItem("LoginEm",$scope.data.username)
                                       console.log($scope.data.username+"=hi-1")
                                       $scope.verifyUserAccountStatus(loginUser,$scope.data.username)
                                       
                                       /* For Unread msgs Count*/
                                       $rootScope.chatCounted=$scope.getAllFriends($scope.data.username);
                                      
                                       }
                                       },
                                       error: function(error) {
                                       $ionicLoading.hide();
                                       var userObj = JSON.parse(error);
                                       showAlert($ionicPopup,"Authentication error",userObj.app42Fault.details);
                                       }
                                       });
              }


          /* For get All Friends */
 $scope.getAllFriends=function(userName){
  $scope.friendsUsrname = []
var buddyService    =   new App42Buddy();
    var result;
    var count=0;
       buddyService.getAllFriends(userName,{
        success: function(object)
        {
          $ionicLoading.hide();
          $scope.friendsUsrname.length = 0;
          var buddyObj = JSON.parse(object);
          result = buddyObj.app42.response.buddies.buddy;
             
          if (result instanceof Array) {
            for(var i = 0;i<result.length;i++)
            {
              var buddyName=result[i].buddyName;
              $scope.friendsUsrname.push(result[i].buddyName);
              
                /* For Unread msgs Count*/
            var chatCount= $scope.getUnreadMsgCount(buddyName,userName);
               count=count+chatCount;
            }
            return count;
          }
          else{
            var buddyName=result.buddyName;
            $scope.friendsUsrname.push(result.buddyName);
            /* For Unread msgs Count*/
           var chatCount= $scope.getUnreadMsgCount(buddyName,userName)
               count=count+chatCount;
               return count;          
          }
          
         
        },
        error: function(error) {
         return count;
          $ionicLoading.hide();
          
        }
      });
    }



/* For Unread msgs Count*/
 $scope.getUnreadMsgCount=function(buddyName,userName){
      
      var key1    ="chatHistory.sender" ,
         key2    = "chatHistory.receiver",
         key3    ="chatHistory.formattedDate",
         value1=buddyName,
         value2=userName,
         value3='Just now';
         var chatCount=0;
         
      var storageService      =   new App42Storage();

      var queryBuilder = new QueryBuilder();
            var q1,q2,query1,query2,query;
            
               q1 = queryBuilder.build(key1, value1, Operator.LIKE);
               q2 = queryBuilder.build(key2, value2, Operator.LIKE);
                query1 = queryBuilder.compoundOperator(q1,Operator.AND, q2);
                query2=queryBuilder.build(key3, value3, Operator.LIKE);
               query = queryBuilder.compoundOperator(query1,Operator.AND, query2);
               storageService.findDocumentsByQuery('STEMERGY','Chatting', query,{  
    success: function(object)   
    {    
        var storageObj = JSON.parse(object);    
        response = storageObj.app42.response.storage.jsonDoc; 
        var chatCount=0;
        if (response instanceof Array) {
                                            for(var i = 0;i<response.length;i++)
                                            {
                                            var chatHistory = response[i].chatHistory; 
                                            if (chatHistory instanceof Array) {                                           
                                            for(var j = 0;j<chatHistory.length;j++)
                                            {
                                            
                                            var chatObj = chatHistory[j]
                                                                                        
                                            if((chatObj.formattedDate)==="Just now"){
                                              chatCount++;
                                              $scope.chatCounting=chatCount;
                                              console.log(chatCount)

                                            }else{
                                             $scope.chatCounting=chatCount;
                                              console.log(chatCount)}
                                          }
                                           } else{
                                            if((chatHistory.formattedDate)==="Just now"){
                                              chatCount++;
                                               $scope.chatCounting=chatCount;
                                              console.log(chatCount)
                                            }else{
                                              $scope.chatCounting=chatCount;
                                              console.log(chatCount)}
                                            
                                           }
                                            }
                                          }else{if((chatHistory.formattedDate)==="Just now"){
                                              chatCount++;
                                               $scope.chatCounting=chatCount;
                                              console.log(chatCount)
                                            }else{
                                              $scope.chatCounting=chatCount;
                                              console.log(chatCount)}
                                          }
        //localStorage.setItem("chatCount",chatCount);
        return chatCount;
    },    
    error: function(error) { 
      return chatCount;
      showAlert($ionicPopup,"Error","No-chats");
   }    
});        

    }

 

asked Jul 4, 2017 in Unity by adhi.ks31 (10 points)
edited Jul 4, 2017 by adhi.ks31

1 Answer

0 votes

Hello Adhi,

 

Please have a look at this link which will help you in your query. 

 

Regards,

Himanshu Sharma

answered Jul 4, 2017 by hs00105 (2,005 points)
Download Widgets
Welcome to ShepHertz Product line forum, where you can ask questions and receive answers from the community. You can also reach out to us on support@shephertz.com
...