Get Record with Request, AngularFire

I am using Firebase, Backend-as-a-Service, for my web application, programming AngularJS.

I want to use db queries to retrieve records, for example: WHERE name == 'The Legend Of Korra'

I have an array of TV shows on a Firebase server, 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 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 } } } 

The code I'm using now to do this:

 $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; }; 

I want to do this without using $firebaseArray and forEach . How can I do it?

Just something like this:

 var seriesRef = new Firebase('http://<<firebaseapp>>.firebaseio.com/series'); var seriesObject = $firebaseObject(seriesRef.query({ name: seriesName })); 
+7
angularjs firebase angularfire
source share
1 answer

I have some suggestions that may help here:

Example

  • Check out this working PLNKR example .
    • I replicated your data in one of my public instances of Firebase.
    • The query you are looking for is seriesCollectionRef.orderByChild("name").equalTo(seriesName)
    • If you enter β€œ Avatar: The Last Airbender ” on the tab and click β€œFind,” you will get the corresponding object in the series.
  • In my example, I extended the $firebaseArray service to include a method to search for a specific series by name.

Factories

 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; } }); }); 

controller

 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); }); }; }); 

No extended service

This is what the controller function looks like if you are not using factories:

  $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."); } }); }; 

rules

Note. It is important to note that you must add ".indexOn":"name" to your Firebase rules in order for the request to be executed efficiently. For example:

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

Hope this helps!

+20
source share

All Articles