首页 > 解决方案 > 函数返回未定义、预期的 Promise 或值 - NodeJS

问题描述

我有一个角度应用程序,它使用 firestore 作为数据库和谷歌云功能来处理后端。当我运行我的应用程序并单击付款以调用Stripe API时,我在云功能的日志中收到以下消息。

函数返回未定义的、预期的 Promise 或值

我一直在阅读几个 stackoverflow 问题,他们谈论我返回 Promise 中的任何内容,.then()但我一直收到同样的错误。好消息是实际值可以毫无问题地存储在 Firestore 中,因此它似乎更像是警告而不是错误,因为没有任何问题。

我错过了什么?

exports.stripeCharges = functions.firestore
  .document("/payments/users/TAMO/{paymentId}")
  .onWrite((event, context) => {
    const payment = event.after.data();
    const paymentId = context.params.paymentId;
    if (!payment || payment.charge) return;

    return admin
      .firestore()
      .doc(`/payments/users/TAMO/${paymentId}`)
      .get()
      .then(snapshot => {
        return snapshot.data();
      })
      .then(customer => {
        const amount = payment.amount * 100;
        const idempotency_key = paymentId;
        const source = payment.token.id;
        const currency = "usd";
        const description = "Test Charge";

        const charges = {
          amount,
          currency,
          description,
          source
        };

        return stripe.charges.create(charges, { idempotency_key });
      })
      .then(charges => {
        return admin
          .firestore()
          .doc(`/payments/users/TAMO/${paymentId}`)
          .set(
            {
              charge: charges
            },
            {
              merge: true
            }
          );
      });
  });

标签: node.jsangulargoogle-cloud-functionses6-promise

解决方案


我通过执行以下操作解决了此警告:

if (!payment || payment.charge) return null;

上面的行检查付款是否存在或是否已经收费


推荐阅读