Firebase身份验证导致云功能返回空

时间:2020-05-22 12:15:09

标签: node.js typescript firebase google-cloud-functions

我有一个firebase函数,该函数应该返回卖方出售的物品。我想通过Firebase身份验证获取卖家的个人资料图片。但是只要我等待该功能

编辑:值得注意的是,mAuth是Firebase身份验证*

await mAuth.geUser(sellerData.UID);

应用程序向我返回了一个空的json或[]

这是该函数的完整代码,该错误发生在第11行或其附近。

export const getHottestItems = functions.region("asia-east2").https.onRequest(async (data, response) => {
    try {
        var arrayItem = new Array<Item>();
        let itemSeller: Seller;
        const sellerSnapshot = await db.collection("users").get();
        // this is the list of promises/awaitables for all items
        const promises = new Array<Promise<FirebaseFirestore.QuerySnapshot<FirebaseFirestore.DocumentData>>>();
        sellerSnapshot.forEach(async (sellerDoc) => {
            const sellerData = sellerDoc.data();
            // THIS PART CAUSES THE API TO RETURN []
            const sellerAuth = await mAuth.getUser(sellerData.UID);
            // check for non null / empty strings
            if (sellerData.Name as string && sellerData.UID as string) {
                // this is all the seller information we need
                itemSeller = new Seller(sellerData.Name, sellerData.UID, sellerAuth.photoURL); // placeholder profile picture
                const refItem = sellerDoc.ref.collection("Items");
                // push all the promises to a list so we can run all our queries in parallel
                promises.push(refItem.get());
            }
        });
        // wait for all promises to finish and get a list of snapshots
        const itemSnapshots = await Promise.all(promises);
        itemSnapshots.forEach((ItemSnapshot) => {
            ItemSnapshot.forEach((ItemDoc) => {
                // get the data
                const itemData = ItemDoc.data();
                // if title is not null, the rest of the fields are unlikely to be.
                if (itemData.Title as string) {
                    // the rest of the logic to convert from database to model is in the constructor
                    arrayItem.push(new Item(ItemDoc.id, itemData.Title, itemSeller, itemData.Likes, itemData.ListedTime, itemData.Rating, itemData.Description, itemData.TransactionInformation, itemData.ProcurementInformation, itemData.Category, itemData.Stock, itemData.Image1, itemData.Image2, itemData.Image3, itemData.Image4, itemData.AdvertisementPoints, itemData.isDiscounted, itemData.isRestocked));
                }
            });
        });
        // sort by performance level
        arrayItem = arrayItem.sort(x => x.Performance);
        if (data.body.userID) {
            arrayItem = await markLikedItems(data.body.userID, arrayItem);
        }
        //send the responseafter all the final modifications
        response.send(arrayItem);
    } catch (err) {
        // log the error
        console.log(err);
        response.status(500).send(err);
    }
});

0 个答案:

没有答案