$q function is not returning any values

Mardymar

When I run this code I am expecting to receive a list of names that I can use in my html. It looks like my function is simply not returning anything and I can't figure out why. I need a few pointers on the proper syntax for using $q on functions that aren't a part of the controller function.

function playerNames($q) {

Parse.$ = jQuery;

Parse.initialize("mykey", "mykey");

var namesdfd = $q.defer();
var names = new Array();
var GameScore = Parse.Object.extend("GameScore");
var query = new Parse.Query(GameScore);

query.find({
    success: function (results) {
        for (var i in results) {
            var name = results[i].get("playerName");
            names.push(name);
        }
        namesdfd.resolve(names);
    },
    error: function (error) {
        namesdfd.reject(data);
    }
});
return namesdfd.promise
    .then(function (results) {
        alert("Results.." + results[1]);
        return results;
    })
    .catch(function (error) {
        alert(error.message);
    });
};

app.controller('NameController', ['$scope', '$q', function($scope, $q) {$scope.names = playerNames($q)}]);
Daniel A. White

You can't directly get the results that way. Assignments are synchronous. Try something like this:

app.controller('NameController', ['$scope', '$q', function($scope, $q) {
   $scope.names = [];
   playerNames($q).then(function(results) {
      $scope.names = results;
   });
}]);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related