Get object property


#1

Hi all

I have a object with 5 properties. Try to print out the object and one property in console:

console.log($scope.dim2)
console.log($scope.dim2.display)

the first one works but the later one is undefined, why is that?

Thanks
Tina


#2

To add on question above, the way i define $scope.dim2.display is:

$tm1Ui.cellGet('FileUpload','Sys Upload Specification Page Definition','CAPEX','0002','SUBNM:Dimension').then(function(returnedData){
     $scope.dim2.display = returnedData.Value;
});

If i changed this part to:
$scope.dim2.display = "Version"

Then the console.log($scope.dim2.display) will give me “Version”.
I am just curious why i would get undefined if using cellGet and assign the value to the property.


#3

Hi twu,

//you need to declare that $scope.dim2 is object first.
$scope.dim2 = {};
//and next is assign a property
$scope.dim2.dimProperty = “something”;

the similar example on StackOverflow:


#4

Hi @twu,

We may need more information on the above. Possible to post the whole section of the code involving these items?


Paul


#5

Hi @plim

below works the same:

Version A:

	$scope.test = new Object();
	$tm1Ui.cellGet('FileUpload','Sys Upload Specification Page Definition','CAPEX','0002','SUBNM:Dimension').then(function(returnedData){
         $scope.test.prop1 = returnedData.Value;
    });
	$tm1Ui.cellGet('FileUpload','Sys Upload Specification Page Definition','CAPEX','0003','SUBNM:Dimension').then(function(returnedData){
         $scope.test.prop2 = returnedData.Value;
    });
	console.log($scope.test.prop1);
	console.log($scope.test.prop2); 

This would give me 2 undefined.

Version B:

    $scope.test = new Object();
	$scope.test.prop1 = "Version";
	$scope.test.prop2 = "";
	console.log($scope.test.prop1);
	console.log($scope.test.prop2);

This would give me correct “Version” and “”.
Not sure what’s the difference?


#6

Hi @twu,

Can you try this out and see what is in the logs:

$scope.test = new Object();
	$tm1Ui.cellGet('FileUpload','Sys Upload Specification Page Definition','CAPEX','0002','SUBNM:Dimension').then(function(returnedData){
         $scope.test.prop1 = returnedData.Value;
         console.log('check 1 %o, %o', returnedData.Value, $scope.test.prop1);
    });
	$tm1Ui.cellGet('FileUpload','Sys Upload Specification Page Definition','CAPEX','0003','SUBNM:Dimension').then(function(returnedData){
         $scope.test.prop2 = returnedData.Value;
         console.log('check 2 %o, %o', returnedData.Value, $scope.test.prop2);
    });


Paul


#7

Hi @plim

That gives me:


#8

Hi @twu,

Thanks for the screenshots.

Version A from your previous post yielded undefined because of sequence/timing issue. Just note that the $tm1Ui function, especially those you append a .then() are ‘promises’ which gets resolved at a later time.

So what happened on your version A is that it executes each lines up to console.log($scope.test.prop2);. But at the time it executes this line, the property $scope.test.prop2 has not been resolved yet, so you will just see undefined in the console.

If you wanted to check the actual value when it is available, you will have to do that within the function passed onto .then().

The last code I have sent is just to check if there is value being returned (empty string) or nothing at all (undefined).

So going back to the original question that you have, is this what you are looking for?


Paul


#9

thanks! got it!