简体   繁体   中英

How to make a firebase function (using on create) return an error that I can catch from my react native app

I have created a Firebase function for my shopping app, where the function is triggered when an order is created and then it checks the quantity of each product in the order and update product quantity in the database. I need this to keep track of how many items is left of each product. However, in case one of the products in the order has more quantity than what left (the quantity of the product in the database), I need a way for the function to return an error I can catch from my react native app so I can inform the user that the quantity he asked for is not available. I also need the function to stop the creating of the order doc in the database.

Here's the firebase function I wrote:

exports.countOrderProductChange = functions.firestore.document("/orders/{id}")
    .onCreate((change, context) => {
      const data = change.data();
      const itemsList = data["itemsList"];
      let error = "";

      const newProductsSizes = {};

      for (const item of itemsList) {
        db.collection("products").doc(item.product.id).get().then((doc) => {
          const product = doc.data();
          let sizes = [];
          if (item.product.id in newProductsSizes) {
            sizes = newProductsSizes[item.product.id];
          } else {
            sizes = product.sizes;
          }

          const remaingSize = sizes.find((size) => (size.name == item.size));
          const remaingSizeQty = remaingSize.qty;

          if (remaingSizeQty < item.qty) {
            if (remaingSizeQty == 0) {
              error = "Sorry there's no more (" + item.size +
               ") size of the product: " + item.product.name;
            } else {
              error = "Sorry there's only "+ remaingSizeQty +
              " of (" + item.size +
              ") size of the product: " + item.product.name;
            }
            functions.logger.log(error);
            return error;
          } else {
            const sizeIndex = sizes.findIndex((obj) => (obj.name == item.size));
            const newQty = remaingSizeQty - item.qty;
            const newSizes = sizes;
            newSizes[sizeIndex].qty = newQty;

            newProductsSizes[item.product.id] = newSizes;
          }
        });
      }
      for (const productId in Object.keys(newProductsSizes)) {
        if (Object.prototype.isPrototypeOf.call(newProductsSizes, productId)) {
          db.collection("products").doc(productId).update({
            sizes: newProductsSizes[productId],
          });
        }
      }
});

As Doug Stevenson commented, your application is not able to directly run the onCreate function, as this type of function is a background function . As shown in the third point, these functions are called from the backend:

When the event provider generates an event that matches the function's conditions, the code is invoked.

You can check this related post for more reference, which also mentions listening to a document used to hold the status of the operation.

Another alternative would be to use a callable function. These functions allow you to handle errors on the client and are called directly within your app.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM