How to do advance filter and sort ( E-commerce ) using Firestore

后端 未结 1 1724
囚心锁ツ
囚心锁ツ 2021-01-28 06:44

I am trying to build an E-commerce app using firebase firestore, i am stuck at filtering and and sorting the product on user input. I am able to apply all filter and sort but I

相关标签:
1条回答
  • 2021-01-28 07:38

    Cloud Firestore queries are immutable. If you try to change the value by calling for example:

    .where('color', '==', colorSelected)
    

    Your query simply becomes a new query. To solve this, you have to create a reference to the allProducts collection and then add a new filter, according to what the user has selected. This is an example:

    const db = firebase.firestore();
    const allProductsRef = db.collection("allProducts");
    const basicQuery;
    if(userHasSelectedColor) {
        basicQuery = allProductsRef.where('color', '==', colorSelected);
    } else if (userHasSelectedColorAndCategory) {
        basicQuery = allProductsRef.where('color', '==', colorSelected)
                        .where('category', 'array-contains-any', keyword);
    }
    

    In this way, you can chain as many function calls as you need.

    0 讨论(0)
提交回复
热议问题