Why this code does not work?

Here is my code. I have marked some places in my code where I don’t get print. I have marked with //THIS IS NOT PRINTED

$scope.showWelcome=false;
	    $scope.showSignIn=true;
		$scope.showLoginError=true;
		$scope.loginUser='';
		
		$scope.signInModal = function() {
						$scope.opts = {
							backdrop: true,
							backdropClick: true,
							dialogFade: false,
							keyboard: true,
							templateUrl: 'app/partials/loginmodal.html',
							controller: ModalSignInInstanceCtrl,
							resolve: {

							} // empty storage
						};



						var modalInstance = $modal.open($scope.opts);

						modalInstance.opened.then(function() {
							console.log("OPENED!");

						});

						modalInstance.result.then(function(scopeVar) {
							$scope.showWelcome=scopeVar.showWelcome;
							console.log("this showWelcome="+scopeVar.showWelcome);
							$scope.showSignIn=scopeVar.showSignIn;
							console.log("this showSignIn="+scopeVar.showSignIn);
							$scope.showLoginError=scopeVar.showLoginError;
							console.log("this showLoginError="+scopeVar.showLoginError);
							$scope.loginUser=scopeVar.loginUser;
							
						
						  var dataObj="grant_type=password&username=xxxxxx&password=vvvvvvvv&scope=openid";
						  
						 
						  
						  CustomerOrderWFService.callTokenAPI(dataObj)
							.then(function onSuccess(response) {
								console.log("Success");
								console.log("result #### = " + JSON.stringify(response));
								var accessToken=response.data.access_token;
								
								console.log("access token #### = " + accessToken);
								
									CustomerOrderWFService.callUserInfoAPI(accessToken)
										.then(function onSuccess(response) {
											console.log("==callUserInfoAPI Success==");
											console.log("result from callUserInfoAPI  = " + JSON.stringify(response));
											
											console.log("user = " + response.data.name);
											$scope.loginUser=response.data.name;
											console.log("login user = " + $scope.loginUser);   // THIS IS PRINTED
											 
											
										}, function onFailure(error) {
											console.log("callUserInfoAPI failure");
									});				
								
							}, function onFailure(error) {
								console.log("callTokenAPI failure");
							});
							
							
							//on ok button press 
							//console.log("in result =" + selectedCustomerAccountId);
							//$scope.newcustomerAccountAssociation.parentCustomerAccountId = selectedCustomerAccountId;
						}, function() {
							//on cancel button press
							console.log("Modal Closed");
						});
					};

					console.log("Here == "+$scope.loginUser);  // THIS IS  NOT PRINTED
					
					

I am clueless why //THIS IS NOT PRINTED
What is going wrong ?

When you say “this is not printed” I’m assuming you mean the text is logged but the contents of $scope.loginUser is an empty string?

This would make sense, looking over your code. The variable is only being set in the then() callback of the CustomerOrderWFService.callUserInfoAPI(accessToken) method. As this method call is asynchronous, the console.log call that comes after it will me made before it’s executed, resulting in the value of $scope.loginUser still being an empty string.

1 Like

text is not logged.
nothing is printed.

Are you getting any kind of errors from your code?

checked again .

No Error.

Nothing is printed for //THIS IS NOT PRINTED

Check the console again then, you’ll most certainly see the line number where you log that empty string to the console (usually at the far right).

Except the logged value isnt empty (It contains the string "Here == ")

It may be a question of clobbering function names, or the fact that your final function doesnt take the variable that it should.

It may be a question of whatever framework you’re loading that defines $scope that we can’t guess at.

It may be a function of code above the pasted bit of code.

True… well then you’d definitively see it in the console. It works for me at any rate.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.