0

I am devolping a website using ionic framework.This is my code:

  $rootScope.$on('$cordovaPush:tokenReceived', function(event, data) {
          storageService.save("deviceToken", data.token);
        });

 var devicetoken =JSON.parse(storageService.get("deviceToken")); 

    HomeOwners.Create(devicetoken).then(function(response) {
        console.log(response.user_id);
    })
alert(devicetoken);

But this alert null.When I refresh page the i t alret me device token value.

Please Help

1
  • could you add storageService code? Commented Sep 5, 2015 at 7:47

2 Answers 2

2

The HTTP request is an asynchronous process, which is worked on in the backgroud. So when you do alert(devicetoken) the request is most likely not done.

By changing the code as little as possible you could call a function inside the $cordovaPush:tokenReceived catch.

$rootScope.$on('$cordovaPush:tokenReceived', function(event, data) {

    storageService.save("deviceToken", data.token);

    weHaveTheDataDoSomething();

});

Since you also tagged this with AngularJS you should probably read a little about resolves and asynchronous processes. One example of resolves: Resolving Data the Right Way

Sign up to request clarification or add additional context in comments.

Comments

0

The point is to wait for the request to finish before trying to access or get any response.

Try this:

var devicetoken;
storageService.get('deviceToken')
 .success(function(response){
    devicetoken = angular.fromJson(response);
    console.log(devicetoken);
}).error(function(response){
  // do something on error with response
});

If this doesn't work, please post the storageService code.

Comments

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.