AngularFire:如何运行连接查询 [英] AngularFire: how to run join queries
问题描述
我的基本数据库概念:
以下是我如何制作我的数据库:
我希望能够显示消息名称,显示标志和先决条件消息的名称(如果已填写)。
我使用$ firebaseArray对象来执行常规列表,并且在看到像这样的结构时查看了如何执行查询的示例:
region_messages:{
xxxxxxx:true,
yyyyyyy:true
}
在我的情况下,我的结构如何呢?我试图使用angularfire查询,我将需要在Swift 3中做同样的。
感谢您的帮助!
这是我的json树,以防您想要运行一些测试:
messages:{
-KXSIeKHTM4lMRbeey2k:{
active:true,
name:link,
type:3,
url:http://www.google.com
},
-KXSIi_qw369nfU28lJJ:{
active:true ,
name:video,
type:4,
url:http://www.youtube.com
} $ b $
region_messages:{
-KXfZYP8e - ZUgaVM9iL:{
-KXSIeKHTM4lMRbeey2k:{
pre_requisite_message_id:,
show_only_once:false
},
-KXSIi_qw369nfU28lJJ:{
pre_requisite_message_id:-KXSIeKHTM4lMRbeey2k,
show_only_once:true
}
}
},
r
-KXfZYP8e - ZUgaVM9iL:{
major:1,
name:Region 1
}
}
$ / code $ / pre
解决方案href =https://youtu.be/Idu9EJPSxiY =nofollow noreferrer>这个视频,我设法解决了这个问题。对我来说,接听电话听起来很奇怪,但我想这个问题并没有多大的麻烦。这个函数:
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $变量rootRef = firebase。 。数据库()REF();
var regionMessagesRef = rootRef.child(region_messages /+ regionId);
$ scope.messages_by_region = []; //这里我重置范围变量
$ b regionMessagesRef.on('child_added',function(rmSnap){
var messageRef = rootRef.child(messages /+ rmSnap .key);
messageRef.once('value')。then(function(msgSnap){
var msg = {
key:msgSnap.key,
名称:msgSnap.val()。name,
类型:$ scope.getTypeName(msgSnap.val()。type),
show_only_once:rmSnap.val()。show_only_once,
pre_requisite_message: ()函数(){
$ scope.messages_by_region.push(msg);
}) ;
})
});
code $
对于swift(iOS),我必须这样做。目前的表现看起来不错。
I'm working in a project where I have these entities: region, messages and a link between region and messages. In this link I also can configure some extra properties such as if the message can be show more than once for a region and if there is a pre-requisite message to be show before.
My basic database concept:
Below is how I'm making my database:
I would like to be able to show a list with the message name, the show flag and the name of the pre-requisite message, if filled.
I'm using the $firebaseArray object to do my regular lists and I have seen examples on how to do the queries when you have a structure like this:
"region_messages":{ "xxxxxxx":true, "yyyyyyy":true }
How could I do in my case, my structure is more complex. I'm trying to query using angularfire and I will need to do the same in Swift 3.
Thanks for any help!
Here is my json tree in case you want to run some tests:
{ "messages" : { "-KXSIeKHTM4lMRbeey2k" : { "active" : true, "name" : "link", "type" : 3, "url" : "http://www.google.com" }, "-KXSIi_qw369nfU28lJJ" : { "active" : true, "name" : "video", "type" : 4, "url" : "http://www.youtube.com" } }, "region_messages" : { "-KXfZYP8e--ZUgaVM9iL" : { "-KXSIeKHTM4lMRbeey2k" : { "pre_requisite_message_id" : "", "show_only_once" : false }, "-KXSIi_qw369nfU28lJJ" : { "pre_requisite_message_id" : "-KXSIeKHTM4lMRbeey2k", "show_only_once" : true } } }, "regions" : { "-KXfZYP8e--ZUgaVM9iL" : { "major" : 1, "name" : "Region 1" } } }
解决方案I followed the instruction in this video and I managed to solve the problem. It sounds weird to me to make subsequent calls but I guess there is not much troubles with it. The function:
$scope.getMessagesByRegion = function(regionId){ var rootRef = firebase.database().ref(); var regionMessagesRef = rootRef.child("region_messages/"+ regionId); $scope.messages_by_region = []; // Here I reset the scope variable regionMessagesRef.on('child_added', function(rmSnap) { var messageRef = rootRef.child("messages/"+rmSnap.key); messageRef.once('value').then(function(msgSnap){ var msg = { key : msgSnap.key, name : msgSnap.val().name, type : $scope.getTypeName(msgSnap.val().type), show_only_once : rmSnap.val().show_only_once, pre_requisite_message : rmSnap.val().pre_requisite_message } $scope.$apply(function(){ $scope.messages_by_region.push(msg); }); }) }); }
For swift (iOS) I had to do the same. The performance seems good so far.
这篇关于AngularFire:如何运行连接查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!