2015-06-01 54 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

非常感謝! –