Tyson Daugherty
Tyson Daugherty

Reputation: 41

angular - how can I get the object from a parse query out of the factory to the controller

Here is the basic code. This code delivers the promise to the controller but I need to get the results object back to the controller.

openupServices.factory('getCurrentProject', 
function($rootScope, $q, $location, $resource) {
    var factory = {};
    factory.getProject = function () {
        var deferred = $q.defer();
        var AppProject = Parse.Object.extend("AppProject");
        var query = new Parse.Query(AppProject);
        query.equalTo("userId", $rootScope.sessionUser.id);
          query.find({
            success: function(results) {
                console.log(results);
          },
          error: function(error) {
            alert("Error: " + error.code + " " + error.message);
          }     
        });
        return deferred.promise;
    }
    return factory;
});


openupControllers.controller('ProjectCtrl',
    function($scope, $location, $rootScope, getCurrentProject) {
        var thisProject = getCurrentProject.getProject(); 
    });

Upvotes: 2

Views: 656

Answers (1)

dfsq
dfsq

Reputation: 193271

First of all you need to resolve and reject promise in service on success and error. Like this:

query.find({
    success: function(results) {
        deferred.resolve(results);
    },
    error: function(error) {
        deferred.reject(error);
    }
});

Then in controller:

getCurrentProject.getProject().then(function(data) {
    $scope.thisProject = data;
});

Upvotes: 1

Related Questions