Aleksandar Mihajlovski Aleksandar Mihajlovski - 5 months ago 54
Javascript Question

Get data from PouchDB callback function?

I need to get all the documents from PouchDB database and store them in $scope variable (AngularJS). Can anyone tell me how to get the 'doc' from the callback function?

db.allDocs({include_docs: true, descending: true}, function(err, doc) {
$scope.info = doc;

});


Outside of this code, $scope.info is undefined, the doc object is not stored in this variable

Answer

I assume that the db is a 3rd party code not part of angularjs or a service, then you need to do it like

db.allDocs({include_docs: true, descending: true}, function(err, doc) {
  $scope.$apply(function(){  
    $scope.info = doc;
  })
});

because angularjs is not aware of changes that happens somewhere else

Comments