我将Firebase,后端即服务用于我的网站应用程序,并通过AngularJS进行编程。
我想使用数据库查询来获取记录,例如: WHERE name == 'The Legend Of Korra'
WHERE name == 'The Legend Of Korra'
我在Firebase服务器中具有电视连续剧数组 JSON CODE :
{ "series" : { "-JkqSvwyDw5F9DvGUu6b" : { "createdAt" : 1426842959884, "creators" : "Michael Dante DiMartino,Bryan Konietzko", "description" : "The series follows the adventures of protagonist twelve-year-old Aang and his friends, who must bring peace and unity to the world by ending the Fire Lord's war against the other three nations.", "episode_reset" : true, "genres" : "Action,Adventure,Fantasy,Comedy-drama", "keywords" : "avatar,the last airbender", "name" : "Avatar: The Last Airbender", "official_website" : "http://nicktoons.nick.com/shows/avatar", "publish_date" : "2005-02-21", "updatedAt" : 1426843055127 } } }
我现在使用的代码来解决这个问题:
$scope.isSeriesValid = function(seriesName) { var seriesRef = new Firebase('http://<<firebaseapp>>.firebaseio.com/series'); var seriesCollection = $firebaseArray(seriesRef); seriesCollection.$loaded(function() { angular.forEach(seriesCollection, function(seriesObject, index) { if(seriesObject.name == seriesName) return true; }); }); return false; };
我想在不使用$firebaseArray和的情况下解决这个问题forEach。我应该怎么做?
$firebaseArray
forEach
就像这样:
var seriesRef = new Firebase('http://<<firebaseapp>>.firebaseio.com/series'); var seriesObject = $firebaseObject(seriesRef.query({ name: seriesName }));
我有一些建议可能会帮助您:
.orderByChild()
.equalTo()
.$ref()
seriesCollectionRef.orderByChild("name").equalTo(seriesName)
Avatar: The Last Airbender
app.factory('SeriesFactory', function(SeriesArrayFactory, fbUrl){ return function(){ var ref = new Firebase(fbUrl+'/series'); return new SeriesArrayFactory(ref); } }); app.factory('SeriesArrayFactory', function($firebaseArray, $q){ return $firebaseArray.$extend({ findSeries:function(seriesName){ var deferred = $q.defer(); // query by 'name' this.$ref().orderByChild("name").equalTo(seriesName).once("value", function(dataSnapshot){ if(dataSnapshot.exists()){ deferred.resolve(dataSnapshot.val()); } else { deferred.reject("Not found."); } }); return deferred.promise; } }); });
app.controller('HomeController',function($scope, SeriesFactory, fbUrl) { $scope.seriesName = ''; $scope.findSeries = function() { console.log("Finding series with name:'",$scope.seriesName,"'"); var seriesCollection = new SeriesFactory(); seriesCollection.findSeries($scope.seriesName).then(function(data){ console.log("Found",data); $scope.series = data; }).catch(function(error){ console.warn(error); }); }; });
如果您不使用工厂,则控制器功能如下所示:
$scope.findSeriesWithoutFactory = function() { var seriesRef = new Firebase(fbUrl+'/series'); var seriesCollection = $firebaseArray(seriesRef); seriesCollection.$ref().orderByChild("name").equalTo($scope.seriesName).once("value", function(dataSnapshot){ var series = dataSnapshot.val(); if(dataSnapshot.exists()){ console.log("Found", series); $scope.series = series; } else { console.warn("Not found."); } }); };
注意:请 务必注意,您应该添加".indexOn":"name"Firebase规则,以使查询高效运行。像这样:
".indexOn":"name"
"yourfirebaseapp": { ".read": "...", ".write": "...", "series": { ".indexOn": "name" } }