javascript - Firestore 在一个事务中读取/更新文档数组
问题描述
我使用 Firestore 创建了库存管理。用户可以更新单个库存项目(产品)并创建采购和发票。当用户创建购买(或发票)应用程序更新多个产品数量时,为每个产品更改创建事件,创建新的购买文档(我存储购买编号和产品数组等等......)并增加“购买编号”。
编码:
const db_user = db.collection("users").doc(userUID);
let products = db_user.collection("products");
let productEvents = db_user.collection("productEvents");
jQuery.each(productsArrayToUpdate, function (index, value) {
let product = products.doc(productsArrayToUpdate[index][0]);
let outQTY = productsArrayToUpdate[index][1];
//...
db.runTransaction(function (transaction) {
return transaction.get(product).then(function (doc) {
if (!doc.exists) {
Swal.fire("Document does not exist!");
}
//...
let newProductQTY = (doc.data().productQTY) ? doc.data().productQTY - outQTY + inQTY : 0 - outQTY + inQTY;
//1. UPDATE PRODUCT DOC WITH NEW QUANTITY
transaction.update(product, {
productQTY: newProductQTY,
//...
});
//2. CREATE NEW DOC IN ANOTHER COLLECTION WITH THIS PRODUCT EVENT
transaction.set(productEvents.doc(), {
productSKU: doc.data().productSKU,
//....
});
});
}).then(function () {
//...
}).catch(function (error) {
//...
});
});
//3. CREATE PURCHASE DOCUMENT FOR ALL
db_user.get().then(function (doc) {
if (doc.exists) {
let purchaseDoc = db_user.collection("purchases").doc();
purchaseDoc.set({
purchaseNumber: purchaseNumber,
purchasePositions: dataTablePurchaseRows,
//...
});
db_user.update({
//INCREMENT PURCHASE DOCUMENT NUMBER
purchaseNumber: firebase.firestore.FieldValue.increment(1)
}).then(function () {
//...
}).catch(function (error) {
//..
});
} else {
//...
}
}).catch(function (error) {
//...
});
它工作正常,但有时它会失败,我的库存很乱:(所以我的问题是如何将文档数组读取/更新并在一个事务中设置新文档(针对每个事件),然后创建我的购买文档如果交易通过,则增加数字。
像这样的东西:
return db.runTransaction(function (transaction) {
productsArrayToUpdate.forEach(function (element,index) {
let product = products.doc(element[0]);
let outQTY = element[1];
...
return transaction.get(product).then(function (doc) {
transaction.update(product, {
productQTY: newProductQTY,
[warehouseIN]: newSelectedWarehouseQTYIN,
productInventoryStatus: inventoryStatus
});
transaction.set(productEvents.doc(), {
productSKU: doc.data().productSKU,
//...
}
}
编辑:我发现了这个 Firebase:Transaction read and update multiple documents
有人可以将其转换为foreach 循环吗:
let promise = await admin.firestore().runTransaction(transaction => {
var post = transaction.get(docRef);
var anotherPost = transaction.get(anotherDocRef);
if (post.exists && anotherPost.exists) {
var newLikes = (post.data().likes || 0) + 1;
await transaction.update(docRef, { likes: newLikes });
newLikes = (anotherPost.data().likes || 0) + 1;
await transaction.update(anotherdocRef, { likes: newLikes });
}
})
解决方案
推荐阅读
- graphql - 使用 nextToken 进行 AppSync GraphQL 查询
- python - 如何对测试数据使用逻辑回归
- sql - 使用 where 子句从 Minioclient 查询 JSON 不起作用
- java - 使用java缩短URL
- python - 如何在 matplotlib 中以更高精度(或更多小数位)在 yaxis 上绘制值?
- excel - 更改聚合公式,输出#NUM
- docker - 如何使用 NGINX 入口控制器从 Cloudflare 恢复原始客户端 IP
- pagination - 在“搜索”连接上请求 500 条记录超出了 graphql 中 100 条记录的“第一”限制
- javascript - Jquery滚动到div的底部
- hyperledger-fabric - OCI 运行时执行失败:container_linux.go:344:启动容器进程导致“没有这样的文件或目录”:未知