Realtime Cloud Storage
Applying filters

  • Feb 27, 2015
  • Starting Guide

Applying filters to search for items in a Realtime Cloud Storage table couldn’t be easier. Knowing that you have the following items at the ProductCatalog table

let’s use the JavaScript SDK to search for a product containing the word Cannondale in its name:

// Preparing the filter reference
filter = { item: "Name", value: "Cannondale" };
tableRef = storageRef.table("ProductCatalog").contains(filter);
 
// Using the filter reference in tableRef to get the item or items
tableRef.getItems(
    function(itemSnapshot) {                                            
          if(itemSnapshot) {
                 console.log("Found item: ", itemSnapshot.val());
          } else {
                 console.log("No more items");
          }
    },                                                           
    function(error) {
            console.log("Get Items error: ", error);
    }
);

NOTE: The getItems success callback will be called for each item that respects the filter criteria using an itemSnapshot object with all the item’s attributes (you get access to the item attributes using the val() method). When there are no more items in the table respecting the desired filter criteria the callback will be called with a null itemSnapshot to signal the end of the async search.

Back to Working with items or proceed to Query and Scan

If you find this interesting please share: