0

https://gyazo.com/e80eea9f69c0cbb52cc7929d0a46ea2f

Name of Object is totalCount
How do I access count from above? I tried: Object.result and it's coming out undefined

Second attempt:

var count = totalCount.result;
for(var i = 0; i < count.length; i++) {
  console.log(count[i])
  // no 'length' property of undefined
}

I totally have no problem accessing this totalCount with angular

<div ng-repeat="num in totalCount.result">
  {{ num.count }} // returns 1
</div>

==========================My Answer======================================== @Norguard mentioned that retrieving data asynchronously will have some implications with loading in the data. So, I put my data in a promise:

exports.totalCount = function ($http) {
    return new Promise(function(resolve, reject) {
        $http.get('/api/count')
                    .success(function (data) {
                        resolve(data)
                    })
                    .error(function (err) {
                        reject(err)
                    })
    })
}

this is the draft of my controller:

exports.countController = function ($scope, $http, totalCount) {
    var c = totalCount.then(function(data) {
        console.log(data[0].count)
    })
}
ken
  • 286
  • 1
  • 5
  • 13
  • 1
    Could you copy the code where you log the object (first image)? Also, don't use Object as a variable. Object is already defined in native javascript. If you are using angular it's likely that you have an "Object" variable inside the controller's scope (something like scope.Object.result), which is different from Object.result – Gerard Jan 28 '16 at 04:37
  • I think Object might be a reserved word in JavaScript. Not sure why it works in angular though... – bri Jan 28 '16 at 04:38
  • What variable are you `console.log()`ing to get that Object in the inspector? – Brett DeWoody Jan 28 '16 at 04:39
  • Sorry about that. I just used object for representation. The name of the object is actually totalCount. – ken Jan 28 '16 at 04:42
  • @BrettDeWoody I want to acces count within result Array – ken Jan 28 '16 at 04:43
  • show where you assign this object to `$scope.Object`.The word `Object` is a console description it can't be the name of a variable – charlietfl Jan 28 '16 at 04:44

2 Answers2

0

If the variable name of the Object is totalCount, then

totalCount.result[0].count

will return the count of 1 from the object example you've shown.

Here's how this works:

  1. totalCount is the object.
  2. result is an array, with 1 item in the example you've shown.
  3. [0] is the first (and only) item in the result array.
  4. count is the property you want.

So totalCount.result[0].count gets the count property, from the 0th item in the result array from the totalCount object.

Brett DeWoody
  • 59,771
  • 29
  • 135
  • 184
  • totalCount.result.length ???? – Jai Jan 28 '16 at 04:46
  • If you wanted to get the number of items in `totalCount.result`, then `totalCount.result.length` would work. If you want to access the `count` property within `totalCount > result > count`, then `totalCount.result[0].count` is what you want to use. – Brett DeWoody Jan 28 '16 at 04:51
0
totalCount.result[0].count;

But that depends on when you're trying to access it and how you're using it.

Norguard
  • 26,167
  • 5
  • 41
  • 49
  • it still says 'Cannot read property '0' of undefined'. Right now I'm just logging it directly into the console. totalCount is loaded from a service that retrieves my counts. does that have some implications? – ken Jan 28 '16 at 04:48
  • 1
    @ken it does if you're loading it asynchronously, and you're trying to access/log it prior to actually retrieving it in a later call-stack. – Norguard Jan 28 '16 at 06:15
  • thanks man! I figured it out. – ken Jan 28 '16 at 06:57