javascript - JS Google Cloud 函数错误 : Reference. 更新失败:第一个参数包含属性中的未定义

标签 javascript firebase-realtime-database google-cloud-functions

这是 JS 中的 Google Cloud 函数,我收到以下错误,该函数成功创建 Stripe 客户,但无法写回数据库。

我猜它与 snap.uid 有关,我是对的吗?如何获取已创建的用于触发该函数的 User 节点的 uid

Error: Reference.update failed: First argument contains undefined in property

exports.createStripeCustomerFromUser = functions.database.ref('/users/{userId}').onCreate((snap, context) => {

  const userSnap = snap.val(); 

  const first = userSnap.firstName;
  const last = userSnap.lastName;
  const email = userSnap.email;
  const description = first + ' ' + last;

  return stripe.customers.create({
    email: email,
    description: description,
    name: description,
  }).then((customer) => {
    var updatedUserData = {};
    updatedUserData[`/stripe_ids/${customer.id}`] = snap.uid;
    updatedUserData[`/stripe_customers/${snap.uid}/customer_id`] = customer.id;
    return admin.database().ref().update(updatedUserData);

  });

});

最佳答案

根据您的情况,请使用

context.params.userId

userId 来自

ref('/users/{userId}')

关于javascript - JS Google Cloud 函数错误 : Reference. 更新失败:第一个参数包含属性中的未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56538089/

相关文章:

firebase - 如何在 Firestore onCreate 触发器中中止文档创建

javascript - Nuxt/Vue - 它生成的 html 和 js 文件

javascript - 找不到 Webpack-dev-server Bundle.js

ios - 用来自服务器的数据填充 UIPageViewController?

Swift:从 Firebase 数据库检索数据以进行标签

python-3.x - 为什么我无法将云函数中的 csv 分隔符设置为管道

javascript - 未添加 Jquery 类

javascript - 如何列出 Express 安装的应用程序的所有 REST 端点?

java - 我想从 firebase 数据库中的单个 Textview 中获取完整数据

javascript - Trigger使用通配符部署云函数失败