无法在Firestore onCreate Cloud功能中获取所有文档

时间:2019-01-18 09:01:04

标签: firebase google-cloud-firestore google-cloud-functions

我正在尝试从馆藏中获取所有文件。有时它会提供所有记录,但有时会返回空白数组或单个记录。

我附上数据库的屏幕快照,以供您参考。下面是我提取文档列表的代码段。

exports.orderEmailLive = functions.firestore
.document('/Root/Live/Orders/Orders_Details/Order_Details/{orderId}')
.onCreate((snap, context) => {

        const orderID = context.params.orderId;
        var order_detail_data = snap.data();

        const promises = [];
        var order_data = [];
        var rest_id = '';
        var customer_id = '';
        var products = [];
        var order_subtotal = 0;
        var order_tax = '';
        var order_service_tax = '';
        var order_tip = 0;
        var order_total = 0;
        var order_date = 0;
        var order_eaten = '';
        var order_taken = '';
        var order_delivery = '';
        var order_id_show = '';
        var special_instructions = '';
        var delivery_instructions = '';
        var customisation = [];
        var orderType = '';
        var deliveryCCharges = '';
        console.log("Order Id",orderID);
        //var query = db.collection('Root').doc('Live').collection('Orders').doc('Orders_Menus').collection('Order_Menus').where('order_id', '==', orderID)
        var orderMenutRef = db.collection('Root').doc('Live').collection('Orders').doc('Orders_Details').collection('Order_Details').doc(orderID).collection('Order_Menus')
          .get()
          .then(snapshot => {

            snapshot.docs.map(function(doc) {
              console.log("Documents => ",doc.data());
                order_data = doc.data();
            //});  

            //snapshot.forEach(order => {
                //order_data = order.data();
                console.log(doc.id, '=>', doc.data());

                //if(order_detail_data.restaurantID){
                    rest_id = order_detail_data.restaurantID;
                    customer_id = order_detail_data.userID;
                    products.push({product_name:order_data.name,product_count:order_data.count,product_price:order_data.price});

                    customisation.push(order_data.selectedCustomizedLabel);
                    //console.log('Customization value',order_data.selectedCustomizedLabel);
                    console.log('Customization',customisation);
                //}
            });
            return console.log('Product Details',products);   
          }).catch(menuError => console.log('Couldnt get Order Menus', menuError));   



    return Promise.all([orderMenutRef]).then(snapshot => {
        //Code to execute
    }).catch(orderError => console.log('Couldnt get orderMenutRef', orderError));       
});

谢谢。

0 个答案:

没有答案