2015-06-01 56 views
1

我试图通过JavaScript SDK从Parse.com数据库查询数据,但指针中的数据未通过。从Parse查询获取指针数据

我的解析数据库中有三个相关的类:问题,会话和_用户。 Questions类有指向提问用户和提交问题的讲话的指针列(“提问”和“讲话”)。

的代码看起来是这样的:

<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

[对象的对象]

[对象对象]

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

谢谢!

+0

'console.log('question',question);' –

回答

1

很高兴找到一个组织良好的问题,包括数据模型的详细信息。它也有一个简单的答案:要访问指向的对象,必须告诉查询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); 
    }); 
} 
+0

非常感谢! –