【问题标题】:Integrating FireStore/Stripe/iOS/Cloud Functions集成 FireStore/Stripe/iOS/Cloud 功能
【发布时间】:2018-12-27 03:54:06
【问题描述】:

我是所有提到的技术的新手。所以,如果这个问题不应该在这里,我提前道歉。

因此,我正在尝试编写与 firebase 提供的示例 stripe-firebase 实时数据库-云功能集成等效的 firestore。它位于这里:https://github.com/firebase/functions-samples/blob/master/stripe/functions/index.js

我成功转换了exports.createStripeCustomer 函数。但我在使用 addPaymentSource 时遇到了问题。

这是我目前所拥有的:

  exports.addPaymentSource = functions.firestore.document('Users/{userId}/paymentSources/{paymentID}').onWrite((change, context) => {
  let newPaymentSource = change.after.data();
  let token = newPaymentSource.token;

  return functions.firestore.document(`Users/${context.params.userId}`).get('customer_data')
      .then((snapshot) => {
        return snapshot.val();
      }).then((customer) => {
        return stripe.customers.createSource(customer, {newPaymentSource});
      }).then((response) => {
        return change.after.ref.parent.set(response);
      }, (error) => {
        return change.after.ref.parent.child('error').set(userFacingMessage(error));
      }).then(() => {
        return reportError(error, {user: context.params.userId});
      });
 });

部署成功,但函数失败并出现以下错误。

TypeError:functions.firestore.document(...).get 不是函数 在exports.addPaymentSource.functions.firestore.document.onWrite (/user_code/index.js:73:77) 在对象。 (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:112:27) 在下一个(本机) 在 /user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71 在 __awaiter (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12) 在 cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:82:36) 在 /var/tmp/worker/worker.js:728:24 在 process._tickDomainCallback (internal/process/next_tick.js:135:7)

显然,'get' 似乎不是一个函数。我从以下参考文档中获得了此功能。 https://firebase.google.com/docs/reference/functions/functions.firestore.DocumentSnapshot

我似乎错过了什么。我已经坚持了几天。因此,任何帮助将不胜感激。提前致谢。

【问题讨论】:

  • 对 firestore 不是很熟悉,但您看过这些文档吗:firebase.google.com/docs/reference/js/… 似乎您可以使用firebase.database().ref(`Users/${context.params.userId}`) 之类的方式获取数据库中某个位置的引用,然后阅读使用.on()的匹配资源。
  • 感谢您的回复。不幸的是,我没有使用实时数据库:(我试图找到与该函数调用相当的firestore。
  • 仅供参考:您可以直接在客户端(web sdk)上集成 w Stripe。根本不需要 Admin SDK。 ...只是确保您不会使事情变得过于复杂... Stripe 在您的/pay 页面中插入一个 iFrame,并给您一个令牌以与新订单一起存储。它符合 PCI 标准。

标签: firebase stripe-payments google-cloud-firestore google-cloud-functions stripe.js


【解决方案1】:

使用firebase-admin

const admin = require('firebase-admin');
exports.addPaymentSource = functions.firestore.document('Users/{userId}/paymentSources/{paymentID}').onWrite((change, context) => {
  let newPaymentSource = change.after.data();
  let token = newPaymentSource.token;

  return admin.firestore().doc(`Users/${context.params.userId}`).get('customer_data')
      // what you have to do
      });
 });

【讨论】:

    【解决方案2】:

    函数包用于触发函数。您可以使用以下方法:

    • onCreate
    • onUpdate
    • onDelete
    • onWrite

    (更多:https://firebase.google.com/docs/firestore/extend-with-functions

    要使用 get() 方法,您需要使用 firebase-admin 包 - 然后您可以从 admin.firestore() 访问 firestore

    (firebase 管理员:https://www.npmjs.com/package/firebase-admin

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-07
      • 2020-03-27
      • 2019-09-26
      • 2019-11-12
      相关资源
      最近更新 更多