0

I am trying to use the following code and use $scope:

var scopes = "https://www.googleapis.com/auth/contacts.readonly";

setTimeout(authorize(), 20);

function authorize() {
    gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: false}, handleAuthorization);
}
invitePeersController.gmailContacts = [];
function handleAuthorization(authorizationResult) {
    if (authorizationResult && !authorizationResult.error) {
        $.get("https://www.google.com/m8/feeds/contacts/default/thin?alt=json&access_token=" + authorizationResult.access_token + "&max-results=50000&v=3.0",
            function(response){
                //process the response here
                console.log(response);
                var jsonChildData = JSON.parse(JSON.stringify( response.feed.entry));
                for(var i=0; i<jsonChildData.length;i++){
                    try {
                        var item = {};
                        var name = JSON.stringify(jsonChildData[i].title.$t);
                        var email = JSON.stringify(jsonChildData[i].gd$email[0].address);

                        if(name.substring(1, name.length-1) && email.substring(1, email.length-1)){
                         item ["name"] = name.substring(1, name.length-1);
                         item ["email"] = email.substring(1, email.length-1);
                         item ["id"] =  email.substring(1, email.length-1).replace(/[^a-zA-Z ]/g, "");
                        invitePeersController.gmailContacts.push(item);
                        }
                }
                catch(err) {
                 // console.log("Something went terribly wrong while trying to fetch Gmail Contacts Data");
                }
            }


            InvitePeersService.setGmailContactsData(invitePeersController.gmailContacts);
                    console.log(invitePeersController.gmailContacts);
                    $scope.$apply(function(){
                        $scope.gmailData = invitePeersController.gmailContacts;
                        console.log($scope.gmailData);
                    })


                });
            }
        }

    }

I can get the response in $scope but cannot get the data elsewhere.

How can I use that value in $scope?

Tried to follow this question, and applied $scope.$apply() but it is not working.

3
  • when you do console.log(invitePeersController.gmailContacts);, does it log data ? Commented May 31, 2017 at 9:31
  • @anoop Yes, it does. Commented May 31, 2017 at 9:31
  • is there any error in console,? are you injecting $scope controller?, or is this a service ?and as a suggestion, you should use $http.get() Commented May 31, 2017 at 9:40

1 Answer 1

0

You need to move the following block:

InvitePeersService.setGmailContactsData(invitePeersController.gmailContacts);
console.log(invitePeersController.gmailContacts);
$scope.$apply(function () {
  $scope.gmailData = invitePeersController.gmailContacts;
  console.log($scope.gmailData);
})

in the

function(response){

block, so that the invitePeersController.gmailContacts is initialized - as the response comes in a callback function.

Therefore:

var scopes = "https://www.googleapis.com/auth/contacts.readonly";

setTimeout(authorize(), 20);

function authorize() {
  gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: false}, handleAuthorization);
}
invitePeersController.gmailContacts = [];
function handleAuthorization(authorizationResult) {
  if (authorizationResult && !authorizationResult.error) {
    $.get('https://www.google.com/m8/feeds/contacts/default/thin?alt=json&access_token=' + authorizationResult.access_token + '&max-results=50000&v=3.0',
      function (response) {
        //process the response here
        console.log(response);
        var jsonChildData = JSON.parse(JSON.stringify(response.feed.entry));
        for (var i = 0; i < jsonChildData.length; i++) {
          try {
            var item = {};
            var name = JSON.stringify(jsonChildData[i].title.$t);
            var email = JSON.stringify(jsonChildData[i].gd$email[0].address);

            if (name.substring(1, name.length - 1) && email.substring(1, email.length - 1)) {
              item ['name'] = name.substring(1, name.length - 1);
              item ['email'] = email.substring(1, email.length - 1);
              item ['id'] = email.substring(1, email.length - 1).replace(/[^a-zA-Z ]/g, '');
              invitePeersController.gmailContacts.push(item);
            }

            InvitePeersService.setGmailContactsData(invitePeersController.gmailContacts);
            console.log(invitePeersController.gmailContacts);
            $scope.$apply(function () {
              $scope.gmailData = invitePeersController.gmailContacts;
              console.log($scope.gmailData);
            })
          }
          catch (err) {
            // console.log("Something went terribly wrong while trying to fetch Gmail Contacts Data");
          }
        }
      });
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Still the same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.