Ionic 4: todo App + firebase
Lección 25 de 38 · 25 Querying Data Building Mobile Apps with Ionic 4 Video · Personas y blogs
25 Querying Data Building Mobile Apps with Ionic 4 Video
in this lesson we are going to learn about querying data from cloud firestore we'll start by understanding how we can use the where method which is offered by
the firebase SDK and then we'll also understand how we can use the order by method which is also a part of the core firebase SDK so let's set that to Visual Studio code and inside Visual Studio
code in the get products function you can see that we are getting all the products from the products collection in a real application you may want to filter these results so instead of
displaying all the data advanced to the user you may want to enforce certain conditions and display the data to the user based on those conditions for example you may only want to display the
products that have a price greater than or less than a certain amount or you may want to order the products by price which is a very common use case and you can do all of this using the where and
the order by functions offered by the firebase SDK if you are coming in from the background of a relational database the ver method works exactly how the where clause in sequel works it lets you
specify certain conditions and filters the result documents that you get from your collection based on those conditions similarly the order by function here
orders the data in a certain order as specified by the arguments that you pass in to the order by function so let's see how we can use the ver function and as an example I'm going to use the wave
function to only get the products that have a price greater than or equal to $900 so all those products that have a price of less than $900 will not be displayed on the page so let's learn how
we can use the where method to use the where method you need to type in dot we're just before executing the gate function and the where method takes in three different arguments the first
argument takes in the name of the field based on which you have a condition here we want to filter the results based on the price of the product which is stored in a field called product price
so the first argument to the wave function will be product price the second argument will be an operator and since we want to retrieve all the products that have a price of greater
than or equal to than a certain value we'll be using the operator greater than or equal to there are other operators to use as well feel free to play around with them the third argument will be the
value that will be used to compare the product price for all the documents in the collection for our case it will be 900 so now the wave function is complete and on this we are executing the gate
function and let's save the file and see what we get and as you can see on the screen we now only get the products that have a price of nine hundred or more all the other
products are not included in the result set if you change this to less than or equal to you will get all the products that have a price of 900 or less let's save
this so now you can see that I only get two products and both these products have a price of 900 or less you can even use multiple wave functions all you have to do is execute the second
wave function after the first one and both the wear conditions will be executed as a logical and also keep in mind that when you are passing in a number for comparison 900 in this case
all the documents in your collection should have the product price property of number type also if the product price property does not exist on any of your document that document will not be
included in your result set so now that we know how we can limit the documents that we get in the result let's understand how we can order the documents using the order by function to
use the order by function just like the wave function you need to execute it before executing the gate function so here I'll chain it with the wave function
and the order by function takes in two different arguments the first argument is the name of the field based on which the documents in the result set will be ordered in our case if we want to sort
the products by price we'll have to pass in product price as the first argument to the order by function and the second argument here specifies the actual order so if you
want to order the results in ascending order then you can pass in ASC and if you want to sort the results in descending order you can pass in des C so here I'll just Square and pass in des
C for now to sort all the products that we get in descending order by price so the product with the most price will be displayed on the top and the product with the least price will be displayed
at the bottom let's save this and you can see that I get the products that have a price of 900 or less ordered by the price and the product with more price is on the top and the product with
the less price is displayed at the bottom we can also comment out the wave function for now so that we get all the products in our collection and let's change the field name to product name to
order all the products by their name and let's also change the order to ascending now we will get all the products in ascending order by name you might be wondering why I get this
iPhone at the end even when it starts with the alphabet hi and this is because the case of the alphabet I in iphone is lowercase so you need to keep in mind that if you are sorting the product by a
field which contains text the case of the alphabets in the text also matters so that's how you can use the order by and the where functions in firebase to filter and sort the results according to
your requirements so now that you know how you can use the firebase authentication and cloud file store features of firebase in the next section we'll start building our project there
to do application