从Parse查询获取指针数据

时间:2015-06-01 18:58:04

标签: javascript parse-platform

我正在尝试通过JavaScript SDK从我的Parse.com数据库查询数据,但是指针中的数据没有通过。

我的Parse DB中有三个相关的类:Questions,Talks和_User。 Questions类有指针列('questioning'和'talk'),指向提问用户和提交问题的谈话。

代码如下所示:

 <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();

输出如下:

  

测试问题1

     

[object Object]

     

[object Object]

问题本身是正确的(测试问题1),而不是用户(或用户ID),它显示[对象对象]。谈话也是如此。知道如何检索和显示这些信息吗?

谢谢!

1 个答案:

答案 0 :(得分:3)

很高兴找到一个组织良好的问题,包括数据模型的细节。它也有一个简单的答案:要访问指向的对象,您必须将查询告诉include它们。那么,这个建议,以及代码中的几点:

// 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);
    });
}