正在从分析查询中获取指针数据
Getting pointer data from Parse query
我正试图通过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);
});
}
相关文章:
- 使用 jQuery 的 .on 函数如何获取事件的原始元素
- jquery试图按名称获取按钮位置
- 如何在jQuery中获取元素的形式
- 如何在php文件中获取$.post-ajax传递的值
- 在Shopify中获取博客文章的图片
- 使用Javascript获取所选选项ID
- 在jQuery中获取表的行索引
- 使用jquery将mysql数据获取到新的表行中
- servlet不工作(也有空指针错误),servlet不能't从javascript中获取值
- 在鼠标指针下获取单词
- 如何从 MSPointerMove 事件获取指针的当前位置
- 限制查询以获取具有特定指针的所有项 (JavaScript - Parse.com)
- 如何获取具有指针字段的对象 ID 的记录
- Parse.com在查询指针数组时出现问题,包括未获取云代码中的嵌套指针数据
- 从Parse中的第二级指针获取数据
- 无法获取指针字段值Parse+React的值
- Javascript通过指针获取函数
- 正在从分析查询中获取指针数据
- 如何从"_User"获取userId作为指针对象
- 在鼠标指针上获取文本框单词