简体   繁体   English

如何使 firebase function(在创建时使用)返回一个我可以从我的反应原生应用程序中捕获的错误

[英]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.我为我的购物应用程序创建了一个 Firebase function,其中 function 在创建订单时触发,然后它会检查订单中每个产品的数量并更新数据库中的产品数量。 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.但是,如果订单中的一个产品数量多于剩余数量(数据库中的产品数量),我需要一种方法让 function 返回一个错误,我可以从我的 react 本机应用程序中捕获,这样我就可以通知用户他要求的数量不可用。 I also need the function to stop the creating of the order doc in the database.我还需要 function 来停止在数据库中创建订单文档。

Here's the firebase function I wrote:这是我写的 firebase function:

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 .正如 Doug Stevenson 评论的那样,您的应用程序无法直接运行onCreate function,因为这种类型的 function 是背景 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.另一种选择是使用可调用的 function。 These functions allow you to handle errors on the client and are called directly within your app.这些函数允许您在客户端处理错误并在您的应用程序中直接调用。

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

相关问题 我可以使用 React-native、firebase 和节点 js 创建应用程序吗? - Can i create an app with React-native, firebase, and node js? 如何将数据从我的数据库 (Firebase Firestore) 发送到我的 React Native 应用程序? - How do I send data from my database (Firebase Firestore) to my React Native app? 使用&#39;create-react-native-app myProject&#39;命令时如何解决错误? - How can solve error while using 'create-react-native-app myProject' command? 如何从 function 返回错误? - How can I return an error from a function? 如何使用 react js 创建一个 react 应用程序? - How can I create a react app using react js? 当我使用 npx create-react-app 制作新的 React 应用程序时出错 - error when i make a new react app using npx create-react-app 如何在我的 React Native 应用程序中创建脚本 - How to create a script within my React Native app 链接承诺。 如何使用 catch 返回错误? - Chaining promisses. How I can use catch to return error? 如何保护音频文件并在我的反应原生应用程序中播放? - How can i protect audio file and play in my react native app? Express上的Firebase-无法通过.catch(错误)返回响应给客户端 - Firebase on Express - Can't return response to client with .catch(error)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM