0

I am working on an angularjs application which uses parse to create an array of names. I then pass that array to a controller, which I use in my html. My problem is that no data is coming through and I'm not sure why. I have tested my Parse connection, and it is working correctly, and I test my controller and it is passing data correctly. It is just when I combine the two that it stops working.

parse.js

function playerNames() {

Parse.$ = jQuery;

Parse.initialize("my key", "my key");

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");
            alert(name);
            names.push(name);
        }
        return names;
    },
    error: function (error) {
        alert(error.message);
        return names;
    }});
};  
app.controller('NameController', ['$scope', function($scope) {$scope.names = playerNames()}]);

index.html

<div class="main" ng-controller="NameController">
<div class="container">
    <table class="table table-hover">
        <thead>
        <tr>
            <th>Name</th>
        </tr>
        </thead>
        <tbody>
        <tr ng-repeat="n in names">
            <td>{{ n }}</td>
        </tr>
        </tbody>
    </table>
</div>
</div>
5
  • 1
    playerNames() doesn't return anything Commented Nov 1, 2015 at 19:26
  • I don't know what you mean. I put the return statements inside query.find. If If I put a return statement at the end, then it would return the array before the query had a chance to populate it since it runs asynchronously. Commented Nov 1, 2015 at 19:52
  • return to success callback does not return to outer function. Use $q to create and return promise Commented Nov 1, 2015 at 19:58
  • Read blog.ninja-squad.com/2015/05/28/angularjs-promises to understand your mistake and fix it Commented Nov 1, 2015 at 20:32
  • You need a good (more between basic and good) understanding of Promises to accomplish what you want to do here. I suggest reading about them online. Commented Nov 1, 2015 at 21:07

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.