简体   繁体   English

如何对Firebase Web App进行子查询以创建表

[英]How to subquery Firebase Web App to Create a table

I'm doing a basic e-commerce web platform, so the user can buy products, and in other page I want to show his product list, and the orders, what I mean is that a user can make one or more orders with 1 or more products. 我正在做一个基本的电子商务Web平台,因此用户可以购买产品,在其他页面上我想显示他的产品列表和订单,我的意思是用户可以用1来完成一个或多个订单。或更多产品。

the UID is the email. UID是电子邮件。 this is my data structure 这是我的数据结构

{
  "Orders" : {
    "-L-Nor4Ld1NNORxNToPI" : {
      "order" : [ {
        "cant" : "1",
        "product_id" : "1"
      }, {
        "cant" : "2",
        "product_id" : "5"
      }, {
        "cant" : "3",
        "product_id" : "8"
      } ],
      "status" : "pendiente",
      "total" : 795,
      "user_id" : "email@gmail.com"
    },
    "-L-UU03CNGSPd8o0UCHn" : {
      "order" : [ {
        "cant" : "2",
        "product_id" : "2"
      }, {
        "cant" : "1",
        "product_id" : "7"
      } ],
      "status" : "pendiente",
      "total" : 280,
      "user_id" : "email@gmail.com"
    },
    "any" : {
      "order" : {
        "product1" : {
          "cant" : 1,
          "product_id" : 1
        },
        "product2" : {
          "cant" : 1,
          "product_id" : 1
        }
      },
      "status" : "delivered",
      "total" : 100.01,
      "user_id" : "email@example.com"
    }
  },
  "Productos" : {
    "-L-JT-TaL5ctRQ-8u8NR" : {
      "price" : 35,
      "product_id" : 1,
      "product_name" : "Serie 100 luces (foco)",
      "stock" : 20
    },
    "-L-JTIcZ69uFkdUQbs8X" : {
      "price" : 75,
      "product_id" : 2,
      "product_name" : "Serie 100 luces (LED)",
      "stock" : 10
    },
    "-L-JTTi8VcLxkgT3RNuA" : {
      "price" : 70,
      "product_id" : 3,
      "product_name" : "Serie 140 luces musical (foco)",
      "stock" : 10
    },
    "-L-JTb0PwTyS8TiWS5W9" : {
      "price" : 96,
      "product_id" : 4,
      "product_name" : "Serie 140 luces musical (LED)",
      "stock" : 10
    },
    "-L-JUm5tF8fYBmR3BZCU" : {
      "price" : 110,
      "product_id" : 5,
      "product_name" : "Serie 300 luces color",
      "stock" : 10
    },
    "-L-JUzclafELx1xjpuQx" : {
      "price" : 135,
      "product_id" : 6,
      "product_name" : "Serie 300 luces blanca",
      "stock" : 10
    },
    "-L-JVB8diTZMmVS3ay1u" : {
      "price" : 130,
      "product_id" : 7,
      "product_name" : "Cascada 400 Luces",
      "stock" : 10
    },
    "-L-JVIgENEJRdcfLfKWT" : {
      "price" : 180,
      "product_id" : 8,
      "product_name" : "Manguera gel 10 metros",
      "stock" : 10
    }
  }
}

And you can see the project by yourself just login with google in this link Go To Project once you have made your order, you can go to this link Order review Page and you should see whats in your order, but that doesnt happens, I want to do something like this 而且您可以看到自己的项目,只需在此链接中登录Google即可进入该项目。一旦您下了订单,就可以转到此链接“ 订单审查”页面 ,您应该会看到订单中有什么,但那没有发生,我想做这样的事情

Get logged user_id then get user_id orders(order_id), then show in (if) multiple tables with 获取记录的user_id,然后获取user_id订单(order_id),然后在(如果)多个表中显示

|Product |Price| Quantity| sub(price*quant)

And its products 及其产品

So as you can see in the code of cart.html im using 因此,正如您在cart.html代码中看到的那样,

ref.orderBy(X).equalTo(uid).on("child_added",function(data){
    ref.orderBy(Y).equalTo(item).on("child_added",function(data2){
        //dothings create table content
    })
    //close table content 
})
//embed table content created.

so I want to show a the table but .then() function doesnt work and the same with .done() , i hope you can help me to understand how should I subquery, to get the user orders in a table 所以我想显示一个表,但是.then()函数不起作用,并且与.done()相同,希望您能帮助我理解如何子查询,以在表中获取用户订单

My solution was not to get values by inner firebase functions, it was getting all the data, then create the table with the data 我的解决方案不是通过内部Firebase函数获取值,而是获取所有数据,然后使用数据创建表

The next code suppose that productsObj, and ordersObj are the result objects of my firebase database products and orders. 下一个代码假定productObj和ordersObj是我的Firebase数据库产品和订单的结果对象。 so the next thing i did was to create dynamically the table with this function. 所以我要做的下一件事是使用此函数动态创建表。 You cannot create subqueries or something like that on firebase functions like on child_added , child_removed , etc.. 您无法on child_addedchild_removedon child_added函数on创建子查询或类似的内容。

function productList(){

    if(ordersObj.length != 0 & productsObj.length == 8){
        //console.log("si termino");    
        $.each(ordersObj,function(index, data){
            var table =('<table class="table table-hover table-bordered">');
            var tableHead = ("<thead class='thead-dark'><tr><th>Producto</th><th>Precio</th><th>Cantidad</th><th>Importe</th></thead>");
            table+=tableHead;
            //console.log(table);
            var tableBody = ("");
            //console.log(tableBody)
            var total = 0;
            $.each(data.order,function(idx, ord){
                console.log(ord.product_id);
                //console.log("precio:"+productsObj[ord.product_id-1].price);
                var subtotal = ord.cant*productsObj[ord.product_id-1].price;
                //console.log("despues del subtotal")
                var row = ('<tr><td>'+productsObj[ord.product_id-1].product_name+'</td><td>'+productsObj[ord.product_id-1].price+'</td>'+'<td>'+ord.cant+'</td>'+'<td>'+subtotal+'</td></tr>');
                //console.log(row);
                total=data.total;
                tableBody+=(row);
            });
                //console.log(tableBody.html());
                //console.log(tableHead.html());
            tableBody+=("<tr><td colspan='2'>Total</td><td colspan='2'>"+total+"</td></tr><tbody></table>");
            table+=tableBody;
            //console.log(table);
            //table.append(tableHead.html());
            $('#content').append(table);
            $('#content').append("<br>");
        });




    }
    else{
        //productList();
    }

} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 FIREBASE phoneAuth (LinkWithPhoneNumber)。 稍后如何在 Firebase Web 中更改用户的关联电话号码。 ( nodejs, Create-react-app )? - FIREBASE phoneAuth ( LinkWithPhoneNumber ) . How to change a user's linked phone number later in Firebase Web. ( nodejs, Create-react-app )? 如何在Firebase Web中创建两个用户组 - How To Create Two User Groups in Firebase Web 如何使用 nuxt firebase 部署 web 应用程序? - How to deploy web app with nuxt firebase? 如何使用Firebase创建动态HTML表? - How to create a dynamic HTML table with Firebase? Tizen Web App中的Firebase - Firebase in Tizen Web App 如何使用 javascript 创建离线 Web 应用程序 - how to create an offline web app using javascript 如何基于位置哈希创建Web应用程序 - How to create a web app based on Location Hash 如何在 FIREBASE WEB javascript 中为用户创建特定的 URI - How to create specific URIs for users in FIREBASE WEB javascript 如何在网络单页应用程序中使用 Firebase Analytics 跟踪页面浏览量? - How to track page view with Firebase Analytics in a web, single page app? 如何在 PHP 网络应用程序上安全地使用 Firebase 身份验证 - How to safely use Firebase Auth on a PHP web app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM