2016-12-07 1 views
0

enter image description herebestimmtes Produkt zu finden, gibt es in Feuerbasis oder nicht

Hallo ich reagieren + Feuerbasis arbeiten werde.

Können Sie mir bitte sagen .. wie Sie herausfinden, ob bestimmte Element in Firebase vorhanden sind oder nicht.

Gleichwie in gegebenen Bild

wie existieren bestimmtes Produkt zu finden, wo productId „-KY5gzllzbAl1jAACcP“

ist

Namen bestimmten Tabelle ist „productcoredetails“.

 var query = firebase.database().ref('ProductCoreDetails'); 
    query.once("value", function(snapshot) { 

     console.log('value of snapshot is', snapshot.val().ProductId); 
     if(snapshot.hasChild(ProductId)){ 
      console.log('PRODUCT EXIST'); 
     } 
     else{ 
      console.log('Product doesnt exist'); 
     } 

    }); 

viele Lösung versucht, aber nichts funktioniert für mich ...

Antwort

1

Siehe docs:

firebase.database().ref('ProductCoreDetails').child(YourProductId).once("value", function(snapshot) { 
     if(snapshot.exists()){ 
      console.log('PRODUCT EXIST'); 
     } 
     else{ 
      console.log('Product doesnt exist'); 
     } 
}) 

Sie können auch das Versprechen basiert (wie das Beispiel in der Dokumentation) verwenden:

firebase.database().ref('ProductCoreDetails').child(YourProductId).once("value") 
.then(function(snapshot) { 
     if(snapshot.exists()){ 
      console.log('PRODUCT EXIST'); 
     } 
     else{ 
      console.log('Product doesnt exist'); 
     } 
}) 
.catch(function(error){console.log('cant retrieve product', error)}) 
Verwandte Themen