简体   繁体   中英

Firestore async function for array of document ids

I have array of firestore document IDS. I need to convert below code to async/await. I usually use async/await for one document but now I need for loop. The last console.log(firestoreProducts) is empty.

function getFirestoreProducts(allSKUsNoDuplicates) {
    //allSKUsNoDuplicates - array of document ids
        let db = firebase.firestore();
        var db_user = db.collection("users").doc(firebase.auth().currentUser.email);
        var products = db_user.collection("products");
        var firestoreProducts = []


        //var products = db_user.collection("products").where("productSKU", "in", allSKUsNoDuplicates).orderBy("productSKU", "asc");// ups only 10 can be in array
        allSKUsNoDuplicates.forEach(function (currentValue, index) {
            //console.log(currentValue)
            products.doc(currentValue)
                .get()
                .then(function (doc) {
                    if (doc.exists) {
                        //console.log("Document data:", doc.data().productSKU);
                        firestoreData = {

                            productSKU: doc.data().productSKU,
                            productBuyPriceNet: (doc.data().productBuyPriceNet) ? doc.data().productBuyPriceNet : 0

                        }
                        firestoreProducts.push(firestoreData);


                    } else {
                        // doc.data() will be undefined in this case
                        console.log("No such document!");
                    }
                }).catch(function (error) {
                    console.log("Error getting document:", error);
                });

        })

        console.log(firestoreProducts)

    }

All you can do is get a list of Promises, for which Promise.all() is a great fit. It completes after all the promises are completed and gives you a list of the results. It is explained further here , as already noted in comments.

In your specific case, this should do it:

async function getFirestoreProducts(allSKUsNoDuplicates) {
    //allSKUsNoDuplicates - array of document ids
        let db = firebase.firestore();
        var db_user = db.collection("users").doc(firebase.auth().currentUser.email);
        var products = db_user.collection("products");
        var firestoreProductPromises = []


        //var products = db_user.collection("products").where("productSKU", "in", allSKUsNoDuplicates).orderBy("productSKU", "asc");// ups only 10 can be in array
        allSKUsNoDuplicates.forEach(function (currentValue, index) {
            //console.log(currentValue)
            firestoreProductPromises.push(
                products.doc(currentValue)
                    .get()
                    .then(function (doc) {
                        if (doc.exists) {
                            //console.log("Document data:", doc.data().productSKU);
                            firestoreData = {

                                productSKU: doc.data().productSKU,
                                productBuyPriceNet: (doc.data().productBuyPriceNet) ? doc.data().productBuyPriceNet : 0

                            }


                        } else {
                            // doc.data() will be undefined in this case
                            console.log("No such document!");
                        }
                    }).catch(function (error) {
                        console.log("Error getting document:", error);
                    }));

        });

        var firestoreProducts = await Promise.all(firestoreProductPromises);

        console.log(firestoreProducts)

    }

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