[英]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.