我一直在寻找不同的示例,但无法为我的火灾基本表找到合适的解决方案。我有3个表1用于问题,第二表包含问题的答案,第三表包含对该答案的评论。
我如何能够使用Firebase执行查询
我一直在寻找不同的示例,但无法为我的火灾基本表找到合适的解决方案。我有3张桌子
问题
答案
评论答案
我如何能够使用Firebase执行查询
mdatabaseReference.child("Answer").equalTo(QID);
我将如何获得特定问题的答案以及对该答案的评论。
这是我的JSON
{
"Answer" : {
"f40357b1-d1f5-4b7a-98ec-54d9e7b2e518" : {
"dateTime" : "16 Mar 2017 15:30:29",
"professorAnswer" : "Hezbollah is an Islamist religious organization founded in 1985 and based in Lebanon. It follows Shi'Islam (also called Shi'ite Islam), the second largest denomination of Islam. Shi'a Muslims follow the teachings of the prophet Muhammad, a direct descendant of Isma'il (the first son of Ibrahim/Abraham).Contd.!",
"professorId" : "7ceef713-eb59-4db4-a8d2-21d5a01eedfc",
"questionId" : "fd2a34a0-e7d9-4b2c-8192-59705df827c2"
}
},
"comment" : {
"29192e3a-a013-4fcc-9859-1f5cc62464cb" : {
"commentText" : "ORGANIZATION hezbollah bases on the bible but their goals is to save people in pagans work!",
"dateTime" : "16 Mar 2017 15:30:52",
"AnswerId" : "f40357b1-d1f5-4b7a-98ec-54d9e7b2e518"
"questionId" : "fd2a34a0-e7d9-4b2c-8192-59705df827c2",
"userId" : "bXCeW6jfidbHuMCCCMkDGWcGZRS2"
}
},
"questions" : {
"41c454a8-fab6-4e41-9093-b1120ffd1be0" : {
"description" : "I know they're a Islamic organization but where are they based? What are their goals?",
"idQuestion" : "fd2a34a0-e7d9-4b2c-8192-59705df827c2",
"time" : "16 Mar 2017 15:30:12",
"title" : "What are the aims of the religious organization Hezbollah?",
"user_id" : "bXCeW6jfidbHuMCCCMkDGWcGZRS2",
}
},
"user" : {
"13bd37e5-fc87-4468-a89e-7cb4ecaab05f" : {
"email" : "email@gmail.com ",
"user_id" : "bXCeW6jfidbHuMCCCMkDGWcGZRS2"
}
}
问题是我想过滤这些事件...例如,使用
.orderByChild("Answer").equalTo(QID)
可能是错误的查询,但这只是概念上仅获取给定问题ID的答案,然后用它填充我的列表。 最佳答案
正确的方法是分别获取每个数据,我不太清楚您的数据是如何组织的,但这可能是一个合适的解决方案:
// Gets the Question with id = QID
mdatabaseReference.child("Questions").child(QID);
// Gets the Answers for that question
mdatabaseReference.child("Answers").child(QID).once("value", function(answers) {
// For every Answer gets the comments
for(var answerID in answers) mdatabaseReference.child("Comments").child(QID).child(answerID);
});
编辑:要有效地使用Firebase,您应该根据想要检索它们的方式来构造数据。如果您想获得给定问题的所有答案,建议您使用以下数据结构:
{
"Answers": {
"questionID": {
"answerID": {
"dateTime" : "16 Mar 2017 15:30:29",
"professorAnswer" : "Hezbollah is an Islamist religious...,
"professorID" : "...",
"questionID" : "..."
}
}
}
}
这样您就可以通过以下方式获取给定QuestionID的数据:
mdatabaseReference.child("Answer").child(questionID).once('value', function(answers) {
// answers contains all the answer for the question with ID == questionID
});
注意:Firebase中没有表,所有内容都是JSON对象