【问题标题】:Concat firebase arrays?Concat Firebase 阵列?
【发布时间】:2016-06-30 00:47:09
【问题描述】:

我正在创建一个 Angular 函数,我想抓取所有类别与单击的按钮匹配的博客文章,我的 Firebase 中有 3 个不同的字段,标题为 category1、category2 和 category3。例如,当用户单击时事通讯时,我想查看具有类别时事通讯的所有帖子,无论它存储在类别 1、类别 2 还是类别 3 中。我以为我可以concat()them。但它不起作用。这是我正在尝试的功能。

$scope.sortByNewsletter = function() {
        var postsNumNews1 = $firebaseArray(ref.child('pPosts').orderByChild("category1").equalTo("Newsletters"));
        var postsNumNews2 = $firebaseArray(ref.child('pPosts').orderByChild("category2").equalTo("Newsletters"));
        var postsNumNews3 = $firebaseArray(ref.child('pPosts').orderByChild("category3").equalTo("Newsletters"));
        console.log(postsNumNews1);
        console.log(postsNumNews2);
        console.log(postsNumNews3);
        var $scope.postsNum = postsNumNews1.concat(postsNumNews2,postsNumNews3);
        console.log($scope.postsNum);
    }

然后我在 postNum 上重复 ng-repeating 以将它们显示到这样的页面。

<div class="blog-item" dir-paginate="post in postsNum | orderBy: 'postedDate': true | filter:searchPosts | itemsPerPage: 1">
                        <img class="img-responsive img-rounded" src="{{ post.imageOne }}" width="100%" alt="" />
                        <div class="blog-content">
                            <h3><strong>{{ post.title }}</strong></h3>
                            <h4><strong>{{ post.subTitle }}</strong></h4>
                            <div class="entry-meta">
                                <span><i class="fa fa-user"></i> {{ post.postedBy }}</span>
                                <span><i class="fa fa-folder"></i> {{ post.category1 }} {{ post.category2 }} {{ post.category3 }}</span>
                                <span><i class="fa fa-calendar"></i> {{ post.datePosted }}</span>
                            </div>
                            <br>
                            <p>{{ post.paragraph1 }}</p>                            
                            <p>{{ post.paragraph2 }}</p>
                            <p>{{ post.paragraph3 }}</p>
               </div>
             </div>

我还认为我可以将多个“类别”传递给我的 orderByChild(),但这也不起作用。

【问题讨论】:

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


    【解决方案1】:

    您正在尝试连接 Firebase 同步数组,这可能是它无法正常工作的原因。您需要做的是将这些元素推送到未绑定到 Firebase 中的值的数组中。另外,您说“我的 Firebase 中有 3 个不同的字段”,您指的是 root/pPosts/ 下的帖子吗?查询时了解数据结构会很有帮助,因此请注意,我通过将其指向 '/pPosts' 创建了 ref:

    $scope.sortByNewsletter = function() {
      $scope.posts = [];
      var ref = new Firebase('https://whatever.firebase.com/pPosts');
      ref.orderByChild('category1').on('value', function(snapshot){
        posts = posts.concat(snapshot);
      });
      ref.orderByChild('category2').on('value', function(snapshot){
        posts = posts.concat(snapshot);
      });
      ref.orderByChild('category3').on('value', function(snapshot){
        posts = posts.concat(snapshot);
      });
    
      console.log($scope.postsNum);
    }
    

    【讨论】:

    • 是的,抱歉,我应该发布一个 JSON 示例。但是,是的,我的所有帖子都生活在“pPosts”下。所以我的结构看起来像这样: 'pPosts': { 'randomKeyFromPush()': { 'title': "blah", 'category1': "newsletter", 'category2': "Announcements", 'category3':"" }}}
    • 我无法让这个方法工作。我总是得到 X 是未定义的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-12
    • 2016-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多