Angularjs 如何在AngularFire中从引用中查询数据

Angularjs 如何在AngularFire中从引用中查询数据,angularjs,firebase,angularfire,Angularjs,Firebase,Angularfire,在post中,可以通过在父记录中输入id并使用如下代码查询来创建子记录: ref.child('/users/{id}/') var students = classesRef.child('classes').child(currentClass.$id).child('students'); students.child(studentRef.name()).set(true); var getStudentsForCurrentClass = function () { ret

在post中,可以通过在父记录中输入id并使用如下代码查询来创建子记录:

ref.child('/users/{id}/')
var students = classesRef.child('classes').child(currentClass.$id).child('students');
students.child(studentRef.name()).set(true);
var getStudentsForCurrentClass = function () {
    return $firebaseArray(classesRef.child('classes').child(currentClass.$id).child('students'));
};
然而,子方法在AngularFire中已经被弃用。当我创建具有父记录的记录时,我创建子记录,然后在父记录中创建引用,如下所示:

ref.child('/users/{id}/')
var students = classesRef.child('classes').child(currentClass.$id).child('students');
students.child(studentRef.name()).set(true);
var getStudentsForCurrentClass = function () {
    return $firebaseArray(classesRef.child('classes').child(currentClass.$id).child('students'));
};
当我尝试使用如下函数查询该记录时:

ref.child('/users/{id}/')
var students = classesRef.child('classes').child(currentClass.$id).child('students');
students.child(studentRef.name()).set(true);
var getStudentsForCurrentClass = function () {
    return $firebaseArray(classesRef.child('classes').child(currentClass.$id).child('students'));
};
这是返回的数组

这是我的数据结构


我得到了一个满是引用的数组,但并没有可以有效处理的数据。我这样做是正确的,还是应该将教室id分配给students表中的一个变量并按该列过滤?

首先,我是最真实的。没有,但这里有一些缺陷

首先,你应该为AngularFire阅读。您将发现
$firebase
$asArray()
都已删除。这已被
$firebaseObject
$firebaseArray
取代

现在要查询数据,只需这样做就简单多了,就像我在所有项目中所做的那样:

// Get the unique id for the user
var uid = $firebaseAuth(<your-firebase-ref>).$getAuth().uid;

// Specify the path we will be working with
var path = uid + '/programs';

// Get all programs, specified the '/' individually
// to remind you to use it after your reference
var programs = $firebaseArray(<your-firebase-ref> + '/' + path);

// After the programs are done loaded, bind them to the scope
// or if this fails for any reason, set an error message to show the user
programs.$loaded()
  .then(function(data) {
    $scope.programs = data;
  })
  .catch(function(err) {
    $scope.error = 'There was an error with the request';
});
//获取用户的唯一id
var uid=$firebaseAuth()。$getAuth().uid;
//指定我们将使用的路径
var path=uid+'/programs';
//获取所有程序,分别指定“/”
//提醒您在参考后使用它
var程序=$firebaseArray(+'/'+path);
//加载完程序后,将其绑定到作用域
//或者,如果由于任何原因失败,请设置一条错误消息以显示用户
程序。$loaded()
.then(功能(数据){
$scope.programs=数据;
})
.catch(函数(err){
$scope.error='请求有错误';
});
另一个有用的功能是监视程序位置的更改,因此如果数据库中有任何更改,它也将更新视图:

programs.$watch(function(event) {

  // Fetch the programs again
  programs = $firebaseArray(<your-firebase-ref> + '/' + path);

  programs.$loaded()
    .then(function(data) {
      $scope.programs = data;
    })
    .catch(function(err) {
      $scope.error = 'There was an error with the request';
  });
});
程序。$watch(功能(事件){
//重新获取程序
程序=$firebaseArray(+'/'+路径);
程序。$loaded()
.then(功能(数据){
$scope.programs=数据;
})
.catch(函数(err){
$scope.error='请求有错误';
});
});

将我的代码更新为最新版本,但我仍然只是得到一系列参考资料,而不是实际记录。我在原始帖子中更新了代码,使用$firebaseArray。我还应该做什么?@jamestrocel您如何解析getStudentsForCurrentClass返回的承诺?现在我解析它时没有by ClasseService的回调,但结果几乎相同。
$scope.$watch('currentClass',function(){if($scope.currentClass){var ref=new Firebase(Firebase\u URI);$scope.studentList=$firebaseArray(ref.child('classes').child($scope.currentClass.$id).child('students');$scope.studentList.$loaded().then(函数(数据){$scope.studentList=data;}).catch(函数(err){$scope.error='请求有错误';});console.log($scope.studentList);}