【问题标题】:Filter data by user with angularfire使用 angularfire 按用户过滤数据
【发布时间】:2016-11-17 22:22:03
【问题描述】:

我正在使用 angular-ui-fullcalendar 来显示和编辑事件。用户可以登录并在登录时拥有唯一的uid。我想用它来区分当前用户制作的事件与其他事件。我想给当前用户事件另一个backgroundColor。

最好的方法是什么??

我尝试了几件事。我的数据如下所示:

```
database
  bookings
    -KWnAYjnYEAeErpvGg0-
       end: "2016-11-16T12:00:00"
       start: "2016-11-16T10:00:00"
       stick: true
       title: "Brugernavn Her"
       uid: "1f17fc37-2a28-4c24-8526-3882f59849e9"
```

我尝试像这样使用当前用户 uid 过滤所有数据

var ref = firebase.database().ref().child("bookings");
var query = ref.orderByChild("uid").equalTo(currentAuth.uid);
var bookings = $firebaseArray(query);
$scope.eventSources = [bookings];

这不会返回任何东西。如果我在第 2 行省略过滤器,它会按预期返回所有预订。但即使过滤器有效,它也不能解决我的问题,因为我想同时获取当前用户事件和所有其他事件。 Firebase 没有“不等于”过滤选项...

我尝试遍历每条记录并比较 uids 并在满足条件时设置 backgroundColor:

var ref = firebase.database().ref().child("bookings");
var bookings = $firebaseArray(ref);
bookings.$ref().on("value", function(snapshot) {
    var list = snapshot.val();

    for (var obj in list) {
        if ( !list.hasOwnProperty(obj) ) continue;
        var b = list[obj];
        if (b.uid === currentAuth.uid) {
            b.className = "myBooking";
            b.backgroundColor = "red";
        }
    }
});
$scope.eventSources = [bookings];

但这会导致异步问题,因此分配给 $scope.eventSources 的“bookings”数组没有被修改。我试图将 $scope.eventSources = [bookings] 移动到异步代码块中,但 FullCalendar 显然无法处理它并且什么也不呈现。

我也试过了,但也没有运气:

bookings.$loaded()
    .then(function(data) {
        $scope.eventSources = [data];
     })
     .catch(function(error) {
         console.log("Error:", error);
     });

我的问题的最佳解决方案是什么?

【问题讨论】:

    标签: angularjs firebase firebase-realtime-database fullcalendar angularfire


    【解决方案1】:

    如果您要修改从 Firebase 加载/同步的数据,您应该扩展 $firebaseArray 服务。通过$loaded() 执行此操作是错误的,因为这只会触发初始数据。

    有关示例,请参阅 Extending $firebaseArray 上的 AngularFire 文档和 Kato 在 Joining data between paths based on id using AngularFire 上的回答。

    【讨论】:

    • 这个用例的一个例子here和here
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多