简体   繁体   English

Firestore promise 在返回产品之前等待产品详细信息

[英]Firestore promise wait for product details before returning products

I know similar questions like this have been asked 1000 times but for the life of me I am struggling with something I feel is quite simple.我知道像这样的类似问题已被问过 1000 次,但在我的一生中,我一直在为一些我觉得很简单的事情而苦苦挣扎。

We have 2 tables, one called order_lines the other called order_lines_meta, I need to first query order_lines and for each line get the order_lines_meta and return that我们有 2 个表,一个称为 order_lines,另一个称为 order_lines_meta,我需要先查询 order_lines 并为每一行获取 order_lines_meta 并返回

I have tried a lot of variations, here is where I am at and stuck, I need it to wait for the order_lines_meta to come back because otherwise I get blank metaData as the data comes after nodejs has already outputted the order_lines我尝试了很多变体,这就是我所处的位置并卡住,我需要它等待 order_lines_meta 回来,否则我会得到空白的元数据,因为数据是在 nodejs 已经输出 order_lines 之后

At the end an object that contains order info, line items of objects and within line items a meta data object最后一个 object 包含订单信息、对象的行项目和行项目内的元数据 object

Appreciate the help, I just can't seem to wrap my brain on this one, and I am certainly open to other ways of doing this as well感谢您的帮助,我似乎无法全神贯注于此,而且我当然也愿意接受其他方法

Using nodejs, express, typescript, firestore使用nodejs, express, typescript, firestore

const orderNumber = req.query.orderNumber as string;
const customerName = req.query.customerName as string;
const orderDate = req.query.orderDate as string;

const pickListObj = {
      orderNumber: orderNumber,
      customerName: customerName,
      orderDate: orderDate,
      line_items: <any>[],
    };

db.collection('order_lines').where('number', '==', orderNumber).get().then((snap) => {
      const promises = <any>[];

      snap.forEach(async (order: any) => {
      // get meta data
        const metaDataObj = <any>[];
        const productName = order.data().name;
        const productQty = order.data().quantity;

        promises.push(db.collection('worder_line_meta').where('lineId', '==', order.data().lineId).get().then((doc: any) => {
          if (doc.display_value != '') {
            const meta = [{display_key: doc.data().display_key, display_value: doc.data().display_value}];
            metaDataObj.push(meta);
          }
        }));
      });
      return Promise.all(promises);
    }).then(() => {
      pickListObj.line_items.push({name: productName, quantity: productQty, meta_data: metaDataObj});
    });

Move the push statement from the last .then inside the previous .then :push语句从最后一个.then上一个.then中:

promises.push(db.collection('worder_line_meta')...then((doc: any) => {
  if (doc.display_value != '') {
    ...
  }
  pickListObj.line_items.push({name: productName,
                               quantity: productQty,
                               meta_data: metaDataObj});
}));

In the last .then , you will then find the complete pickListObj .在最后一个.then中,您将找到完整的pickListObj

However, I wonder whether it might be simpler and faster to join the two database collections right on the database and retrieve everything with one db.collection operation.但是,我想知道在数据库上加入两个数据库 collections 并通过一个db.collection操作检索所有内容是否更简单、更快。

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

相关问题 在返回所有异步函数以更新 firestore 文档之前,如何等待 NodeJS 中的多个异步函数? - How do I wait for multiple async functions in NodeJS before returning them all to update a firestore document? 如何在运行函数之前等待数据从 Firestore 加载 Angular - How to wait for data to be loaded from Firestore before running functions Angular 在返回之前等待完成处理程序完成 - Wait for completion handler to finish before returning Promise 存在数据时从 Firestore 返回空值 - Promise returning empty value from Firestore when data is present 如何等待 firestore 数据库在返回数据之前获取数据 - How to await for firestore database to fetch data before returning it 在viewcontroller中更新tableView之前等待单独的数据class从firestore获取数据? - Wait for separate data class to get data from firestore before updating tableView in viewcontroller? firestore function 只返回 promise - firestore function only returns a promise Flipkart 和 AmazonAPI 获取产品详情和当前产品报价 - Flipkart and AmazonAPI for Product Details and current Product Offers 如何等待 Firestore 索引部署? - How to wait for Firestore indexes to deploy? 使用 Cloud Firestore 进行高级产品过滤 - Advanced product filtering with Cloud Firestore
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM