正在从分析查询中获取指针数据

Getting pointer data from Parse query

本文关键字:获取 指针 数据 查询      更新时间:2023-09-26

我正试图通过JavaScript SDK从Parse.com数据库中查询数据,但指针中的数据无法通过。

在我的分析数据库中有三个相关的类:Questions、Talks和_User。Questions类有指针列("提问"answers"谈话"(,它们指向提问用户和问题提交到的谈话。

代码如下:

 <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([object object]。谈话也是如此。知道如何检索和显示这些信息吗?

谢谢!

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