首页 > 解决方案 > 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 });
  }
})

标签: javascriptgoogle-cloud-firestore

解决方案


推荐阅读