简体   繁体   中英

Firebase Query (multiple value)

How do I perform a query in firebase with multiple query? Which mean I like to give multiple value to compare in database and get the match result.

var db = firebase.firestore();
    var routeRef = db.collection("RouteInfo");
    var queryResult = routeRef.where(("origin", "==", origin1) && ("destination", "==", destination1) && ("departureDate","==" , departureDate1 )).get().then(function(snapshot){
        snapshot.forEach(function(doc){
            $("#routeResult").append(/*something inside to show the match result*/);
        });
    })
    .catch(function(error){
        console.log("Error fetting documents: ", error);
    });

Sorry I had no experience in web development.

There is no way you can add multiple where() function calls using && operator. So to solve this, please change the following line of code:

var queryResult = routeRef.where(("origin", "==", origin1) &&
    ("destination", "==", destination1) &&
    ("departureDate","==" , departureDate1 )).get().then(function(snapshot){/* ... */}

to

var queryResult = routeRef.where(("origin", "==", origin1)
    .where("destination", "==", destination1)
    .where("departureDate","==" , departureDate1)).get().then(function(snapshot){/* ... */}

Chaining multiple where() functions, as explained above, works perfectly fine when it comes to Firestore.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM