Lucas Kunzler
Lucas Kunzler

Reputation: 109

Getting pointer data from Parse query

I'm trying to query data from my Parse.com database through the JavaScript SDK but the data from a pointer is not coming through.

There are three relevant Classes in my Parse DB: Questions, Talks and _User. The Questions class have pointers columns ('questioning' and 'talk') which point to the questioning user and the talk the question was submitted to.

The code looks like this:

 <script type="text/javascript">
  Parse.initialize("PARSE APP ID", "PARSE JS KEY");
  var Questions = Parse.Object.extend("Questions");

    function getPosts(){
      var query = new Parse.Query(Questions);
                query.equalTo("active", true);
                query.descending("CreatedAt");
                query.find({

        success: function (results){
          var output = "";
            for (var i in results){
                var talk = results[i].get("talk");
                var question = results[i].get("question");
                var questioning = results[i].get("questioning");
                var talk = results[i].get("talk");
                output += "<li>";
                output += "<h3>"+question+"</h3>";
                output += "<p>"+questioning+"</p>";
                output += "<p>"+talk+"</p>";
                output += "</li>";
            }
            $("#list-posts").html(output);
        }, error: function (error){
            console.log("Query Error:"+error.message);
        }
      });
    }


    getPosts();

And the output looks like this:

Test Question 1

[object Object]

[object Object]

The question itself is correct (Test Question 1) but instead of the user (or user id) it's showing [object Object]. Same thing for the Talk. Any idea how to retrieve and show this information?

Thanks!

Upvotes: 4

Views: 3675

Answers (1)

danh
danh

Reputation: 62686

It a pleasure to find a well organized question, including details of the data model. It has a simple answer, too: to access the pointed-to objects, you must tell the query to include them. So, that advice, and a couple more points in the code:

// see point below about for..in array iteration
// strongly suggest underscorejs, that has loads of other features
var _ = require('underscore');

function getPosts(){
    var query = new Parse.Query(Questions);
    query.equalTo("active", true);

    // order by creation is default, and createdAt is spelled with a lowercase 'c'
    //query.descending("CreatedAt");

    // these will fix the problem in the OP
    query.include("questioning");
    query.include("talk");

    // its a good habit to start using the promise-returning
    // varieties of these functions
    return query.find();
}

function updatePostList() {
    getPosts().then(function (results) {
        var output = "";
        // most authors recommend against for..in on an array
        // also, your use of var i as the index into results is incorrect
        // for (var i in results){  <-- change this to use _.each
        _.each(results, function(result) {
            var talk = result.get("talk");
            var question = result.get("question");
            var questioning = result.get("questioning");
            output += "<li>";
            output += "<h3>"+question+"</h3>";
            output += "<p>"+questioning+"</p>";
            output += "<p>"+talk+"</p>";
            output += "</li>";
        });

        // a good example of the value of underscore, you could shorten
        // the loop above by using _.reduce

        $("#list-posts").html(output);
    }, function (error) {
        console.log("Query Error:"+error.message);
    });
}

Upvotes: 4

Related Questions