【问题标题】:How to fix erorr, Unhandled Rejection (FirebaseError): Function DocumentReference.set() called with invalid data. Unsupported field value: undefined如何修复错误,未处理的拒绝 (FirebaseError):使用无效数据调用的函数 DocumentReference.set()。不支持的字段值:未定义
【发布时间】:2021-10-26 01:54:53
【问题描述】:

我有一个用于付款处理的篮子,并且 Stripe 功能运行良好,尽管收到了 firebase 错误,但付款仍然通过,我想要完成的是将数据推送到 firebase 后端而不是创建订单历史记录,但数据不是'没有被推入firebase集合。控制台日志错误表明第 47 行 const payload = await stripe 和第 56 行 db.collection("users") 存在问题,因此 firebase 读取用户 ID 的方式显然存在问题。有什么建议吗?

const handleSubmit = async (event) => {
    // do all the fancy stripe stuff...
    event.preventDefault();
    setProcessing(true);

    const payload = await stripe
      .confirmCardPayment(clientSecret, {
        payment_method: {
          card: elements.getElement(CardElement),
        },
      })
      .then(({ paymentIntent }) => {
        // paymentIntent = payment confirmation

        db.collection("users")
          .doc(user?.uid)
          .collection("orders")
          .doc(paymentIntent.id)
          .set({
            basket: basket,
            amount: paymentIntent.amount,
            created: paymentIntent.created,
          });

        setSucceeded(true);
        setError(null);
        setProcessing(false);

        dispatch({
          type: "EMPTY_BASKET",
        });

        history.replace("/orders");
      });
  };

错误图片

【问题讨论】:

    标签: javascript reactjs firebase google-cloud-firestore stripe-payments


    【解决方案1】:

    当您将任何undefined 值传递给文档时会引发错误。我建议在 set() 方法之前添加一些 console.log() 语句并检查未定义的字段。此外,您没有处理 set() 方法返回的承诺。尝试将您的代码重构为:

    const payload = await stripe
      .confirmCardPayment(clientSecret, {
        payment_method: {
          card: elements.getElement(CardElement),
        },
      })
      .then(async ({ paymentIntent }) => {
        //  ^^^^^
        // paymentIntent = payment confirmation
        try {
          console.log("Payment Intent:", paymentIntent);
          console.log("Basket:", basket);
          // await here
          await db
            .collection("users")
            .doc(user?.uid)
            .collection("orders")
            .doc(paymentIntent.id)
            .set({
              basket: basket,
              amount: paymentIntent.amount,
              created: paymentIntent.created,
            });
    
          // proceed
        } catch (e) {
          console.log(e);
        }
      });
    
    

    【讨论】:

      猜你喜欢
      • 2021-10-05
      • 2019-12-07
      • 1970-01-01
      • 2020-04-24
      • 2019-03-08
      • 2023-01-04
      • 2021-12-26
      • 2020-06-13
      相关资源
      最近更新 更多