0

In the code below, I'd like the $scope.persons variable to consist of an array of objects that I GET from CouchDB.

.controller('personsCtrl', ['$scope', 'personSrv', function personsCtrl($scope, $http) {
    $scope.persons = function(){
      return $http.get('http://localhost:5984/quotes/_all_docs?include_docs=true')
          .then(function(response) {
              if (typeof response.data === 'object') {
                  return $.map(response.data, function(el){return el});
              } else {
                  // invalid response
                  return $q.reject(response.data);
              }

          }, function(response) {
              // something went wrong
              return $q.reject(response.data);
          });
    }
}])

So that I can display the information on my page like this:

<div>
    <p class="lead">Persons</p>
    <ul>
        <li ng-repeat="p in persons"><a ng-href="#/quotes/{{ p.doc.dataid }}">{{ p.doc.dataname }}</a></li>
    </ul>
</div>

Each object/person has an id, a name and a quote. I tried using $.map to convert the data I got from the GET to an array, but it doesn't seem to work...

3
  • 1
    What is the markup between the objects to separate them? Because if the returns is only one object, you can simple $scope.persons.push(response.data) Commented Oct 31, 2016 at 22:12
  • Is the object that is returned on the promise an array nested within an object literal or where you saying that it's an array with multiple object literals? What does the data structure look like that is in response.data? Commented Oct 31, 2016 at 22:12
  • I'm not quite sure; I used to be able to use data.rows to convert the data I got back from a GET call to an array. I suppose it returns the documents from CouchDB as a JSON object. Commented Oct 31, 2016 at 22:21

0

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.