1

I want to search through my firebase database, so the users are able to find the product they are searching for.

Currently I'm matching the data property this.searchText with the product.title from the firebase.

return str.filter((product) => {
    return product.title.match(textSearch)
})

I have a problem with this:

If the databases contains 1000 products, it doesn't make sense to filter the products on the site, but rather make a effective query like:

firebase.database.ref('products').containing('leather shoe')

I just can't find the right solution for this.

  • For 1.: As the code is identical (besides the string), there's no way to give any hints. You should provide some example data and the surrounding methods where the filter is used. – Bennett Dams Aug 02 '18 at 11:45
  • Of course.. In the meantime while waiting for answers I finally found the solution to the first question (overseen old method). I've edited the question now. – Lukas Benediktson Aug 02 '18 at 11:56
  • how do you get the list of products from firebase? – Guy S Aug 02 '18 at 12:22

1 Answers1

0

Firebase.database.ref('products') returns a Reference and there is no containing() method for a Reference.

The method that is the most similar is equalTo(), as detailed here.

If the filed which hold the value "leather shoe" is, let say, the "type" field, you would do something like:

var ref = firebase.database().ref("products");
ref.orderByChild("type").equalTo("leather shoe").on("child_added", function(snapshot) {
  console.log(snapshot.key);
});

With vuefire you would do:

var firebaseApp = firebase.initializeApp({ ... })
var db = firebaseApp.database()

var vm = new Vue({
  el: '#demo',
  firebase: 
    anArray: db.ref("products").orderByChild("type").equalTo("leather shoe")
  }
})
Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
  • but the problem i found with this solution was that the user has to type exactly the title stored in firebase. Lets say the user types "leather" - then the shoe stored as "leather shoe" won't be retrieved. @RenaudTarnec – Lukas Benediktson Aug 02 '18 at 16:04
  • Yes indeed. There is no way to query the Real Time Database similarly to a LIKE in SQL. There are some workarounds as you can find in the following SO post: https://stackoverflow.com/questions/22506531/how-to-perform-sql-like-operation-on-firebase – Renaud Tarnec Aug 02 '18 at 16:14