我正在尝试通过 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();
输出看起来像这样:
Test Question 1
[object Object]
[object Object]
问题本身是正确的(测试问题 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);
});
}
关于javascript - 从 Parse 查询中获取指针数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30581121/