本文介绍了Firebase - 按列过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Angular.js 应用程序使用 Firebase.

I'm using Firebase for my Angular.js application.

我正在为 Firebase 寻找与 SQL 的 WHERE 语句等效的语句.

I'm looking for the equivalent of SQL's WHERE statement for Firebase.

我在 Firebase 中存储了一系列电视剧,我只想获取那些具有用户输入名称的电视剧(在示例 searchQuery 中).

I have an array of TV series stored in Firebase, and I want to fetch only these that has the name that the user entered (in the example searchQuery).

Firebase 支持吗?有这样的吗?

Does Firebase support it? Does it have something like this?

var seriesRef = new Firebase('http://{app}.firebaseio.com/series');
var seriesObject = $firebaseObject(seriesRef.query({ name: searchQuery }));

推荐答案

我有一些建议可能对这里有所帮助:

I have some suggestions that may help here:

  • 查看 Firebase 查询文档.具体来说,
    • 查看这个有效的 CodePen 演示.
      • 我在我的一个公共 Firebase 实例中复制了您的数据.
      • 您要查找的查询是 seriesCollectionRef.orderByChild('name').equalTo(seriesName)
      • 如果您在输入中输入Avatar: The Last Airbender"并点击Find",您将获得匹配的系列对象.
      • Check out this working CodePen demo.
        • I replicated your data in one of my public Firebase instances.
        • The query that you're looking for is seriesCollectionRef.orderByChild('name').equalTo(seriesName)
        • If you enter 'Avatar: The Last Airbender' in the input and click "Find", you'll get the matching series object.
        app.factory('SeriesFactory', function(SeriesArrayFactory, fbUrl) {
          return function() {
            const ref = new Firebase(`${fbUrl}/series`);
            return new SeriesArrayFactory(ref);
          }
        });
        
        app.factory('SeriesArrayFactory', function($firebaseArray, $q) {
          return $firebaseArray.$extend({
            findSeries: function(seriesName) {
              const deferred = $q.defer();
        
              // query by 'name'
              this.$ref()
                .orderByChild('name')
                .equalTo(seriesName)
                .once('value', function(dataSnapshot) {
                  if (dataSnapshot.exists()) {
                    const value = dataSnapshot.val();
                    deferred.resolve(value);
                  } else {
                    deferred.reject('Not found');
                  }
                })
        
              return deferred.promise;
            }
          });
        });
        

        控制器

        app.controller('HomeController',function($scope, SeriesFactory, fbUrl) {
          $scope.seriesName = '';
        
          $scope.findSeries = function() {
            const seriesCollection = new SeriesFactory();
        
            seriesCollection
              .findSeries($scope.seriesName)
              .then(function(data) {
                $scope.series = data;
              })
              .catch(function(error) {
                console.error(error);
              });
          };
        });
        

        无扩展服务

        如果您不使用工厂,控制器函数的外观如下所示:

        Without Extended Service

        Here is what a controller function would look like if you weren't using the factories:

        $scope.findSeriesWithoutFactory = function() {
          const seriesRef = new Firebase(`${fbUrl}/series`);
          const seriesCollection = $firebaseArray(seriesRef);
        
          seriesCollection.$ref()
            .orderByChild('name')
            .equalTo($scope.seriesName)
            .once('value', function(dataSnapshot) {
              if (dataSnapshot.exists()){
                $scope.series = dataSnapshot.val();
              } else {
                console.error('Not found.');
              }
            });
        };
        

        规则

        注意:请务必注意,您应该将 ".indexOn": "name" 添加到 Firebase 规则中,以便查询高效运行.请参阅 Firebase 索引您的数据部分="https://www.firebase.com/docs/security/guide/" rel="nofollow noreferrer">安全&规则指南了解更多信息.下面是一个例子:

        Rules

        Note: It's important to note that you should add ".indexOn": "name" to your Firebase rules so that the query runs efficiently. See the Indexing Your Data portion of the Firebase Security & Rules Guide for more information. Below is an example:

        "yourfirebaseapp": {
          ".read": "...",
          ".write": "...",
          "series": {
            ".indexOn": "name"
          }
        }
        

        这篇关于Firebase - 按列过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 12:35