使用关系外键设置的 Angularfire nosql 查询
Angularfire nosql query using relational foreign keys setup
我在尝试使用 angular 通过 firebase 进行关系查询时遇到了很多困难。
var userChallengeRef=new Firebase(FIREBASE_URL+ 'users/'+$rootScope.currentUser.$id+'/mychallenges');
var userChallengeInfo=$firebaseArray(userChallengeRef)
// var log = [];
// var challUserRef=challengesRef
// challUserRef.orderByChild("participants").equalTo($rootScope.currentUser.$id).on("child_added", function(snapshot){
// })
// var challUserInfo=$firebaseArray(challUserRef);
$scope.mychallenges=[];
// angular.forEach(userChallengeRef, function(value, key) {
// // this.push(key + ': ' + value);
// var temp=new Firebase(FIREBASE_URL+ 'challenges/'+value);
// $scope.mychallenges.push(temp)
// }, log);
userChallengeInfo.$loaded()
.then(function(){
angular.forEach(userChallengeInfo, function(userChallengeInfo) {
// console.log(userChallengeInfo);
// console.log(userChallengeInfo.$id);
var tempRef=new Firebase(FIREBASE_URL+ 'challenges/'+userChallengeInfo.$id)
var temp=$firebaseArray(tempRef);
console.log(userChallengeInfo.role)
temp.$loaded().then(function(){
console.log(temp.name)
})
// console.log(temp);
// $scope.mychallenges.push(temp);
$scope.mychallenges.push({
id: userChallengeInfo.$id,
name: 'test object',
name2: 'temp.$name'
date: temp.date,
role: temp.challengeParticipant.role,*/
})
})
console.log($scope.mychallenges);
});
我尝试了很多不同的方法都没有成功。温度显示为一个奇怪的对象。我想通过仅输入 firebase 数组 foreach 循环中具有用户标识的对象来生成一个 angular 数组。然后我使用 ng-repeat 使其工作。我宁愿不使用过滤器,因为如果数据库变大,这种方法会快得多。但是,我愿意接受有关最佳实践的任何建议。
如果我的 mychallenges 关系 firebasearray 实现像 "userChallengeInfo" 数组一样正常工作,它将在日志中显示如下:
Object {date: 1455391999128, role: "Participant", $id: "-KAHXgh5_j-4PPnyjNP4", $priority: null}
请注意,我并不想在执行此操作时使用 firebase 数组,因为我希望这是一个未存储在数据库中的临时本地对象。
解决方案:经过一些不同的实验,我发现如果我改变
var temp=$firebaseObject(tempRef);
然后我可以将它推入 angular 本地数组以正确调用不同的属性。现在我可以像 temp.name.
这样调用 temp 的属性
我想我无法将 firebasearray 推送到 angular 数组,但我可以将 firebaseobject 推送到它上面。我知道 firebasearray 通常在对象需要调用保存方法时自动更新数据库。
我在尝试使用 angular 通过 firebase 进行关系查询时遇到了很多困难。
var userChallengeRef=new Firebase(FIREBASE_URL+ 'users/'+$rootScope.currentUser.$id+'/mychallenges');
var userChallengeInfo=$firebaseArray(userChallengeRef)
// var log = [];
// var challUserRef=challengesRef
// challUserRef.orderByChild("participants").equalTo($rootScope.currentUser.$id).on("child_added", function(snapshot){
// })
// var challUserInfo=$firebaseArray(challUserRef);
$scope.mychallenges=[];
// angular.forEach(userChallengeRef, function(value, key) {
// // this.push(key + ': ' + value);
// var temp=new Firebase(FIREBASE_URL+ 'challenges/'+value);
// $scope.mychallenges.push(temp)
// }, log);
userChallengeInfo.$loaded()
.then(function(){
angular.forEach(userChallengeInfo, function(userChallengeInfo) {
// console.log(userChallengeInfo);
// console.log(userChallengeInfo.$id);
var tempRef=new Firebase(FIREBASE_URL+ 'challenges/'+userChallengeInfo.$id)
var temp=$firebaseArray(tempRef);
console.log(userChallengeInfo.role)
temp.$loaded().then(function(){
console.log(temp.name)
})
// console.log(temp);
// $scope.mychallenges.push(temp);
$scope.mychallenges.push({
id: userChallengeInfo.$id,
name: 'test object',
name2: 'temp.$name'
date: temp.date,
role: temp.challengeParticipant.role,*/
})
})
console.log($scope.mychallenges);
});
我尝试了很多不同的方法都没有成功。温度显示为一个奇怪的对象。我想通过仅输入 firebase 数组 foreach 循环中具有用户标识的对象来生成一个 angular 数组。然后我使用 ng-repeat 使其工作。我宁愿不使用过滤器,因为如果数据库变大,这种方法会快得多。但是,我愿意接受有关最佳实践的任何建议。
如果我的 mychallenges 关系 firebasearray 实现像 "userChallengeInfo" 数组一样正常工作,它将在日志中显示如下:
Object {date: 1455391999128, role: "Participant", $id: "-KAHXgh5_j-4PPnyjNP4", $priority: null}
请注意,我并不想在执行此操作时使用 firebase 数组,因为我希望这是一个未存储在数据库中的临时本地对象。
解决方案:经过一些不同的实验,我发现如果我改变
var temp=$firebaseObject(tempRef);
然后我可以将它推入 angular 本地数组以正确调用不同的属性。现在我可以像 temp.name.
这样调用 temp 的属性我想我无法将 firebasearray 推送到 angular 数组,但我可以将 firebaseobject 推送到它上面。我知道 firebasearray 通常在对象需要调用保存方法时自动更新数据库。