Search for a field in firebase

0

I have my database in firebase as you can see here

The user scans a product and brings you the number of this product, which is put in an input, then when you click on search you must bring the description and the value of that item and show it somewhere from the front

How would the function or query be so that when the person click on search brings me the requested data? I know it's a simple query but I do not have it very clear I'm new to ionic

I tried to do this to see if something returned to me on the console but nothing

findproduct(){
   var ref = firebase.database().ref("/productos/");
ref.orderByChild("Producto").equalTo(1706).on("child_added", function(snapshot) {
  console.log(snapshot.key);
});
  }
    
asked by user89984 06.06.2018 в 19:51
source

1 answer

0

What you can do is use another data structure.

If you use:

{
"pructosbd": {
    "productos": {
        "7702158351852": {
            "descripcion": "...",
            "valor": "86.700.00",
            "id": "1"
        },
        "1706": {
            "descripcion": "...",
            "valor": "00.00",
            "id": "2"
        },
        "1137": {
            "descripcion": "...",
            "valor": "00.00",
            "id": "3"
        },
    }
}

}

I do not know exactly what the end of the field id in your structure, if it is not important producto should be your new ID

With the new structure you can make a query of this type:

findproduct(){
var ref = firebase.database().ref("/productos/" + "1706").once('value').then(function(snapshot) {
    console.log(snapshot.val());
});

}

Of course, in order for the product code to work, it should be unique. Being unique you will only need to search in the path of your products, if the product exists, you will have all the information available, otherwise the answer will be null.

You can find information related to this topic at: link

    
answered by 07.06.2018 в 20:01