.onCreate云函数触发后如何更新或设置Firestore文档的属性 [英] How to update or set property of firestore document after .onCreate cloud function trigger

本文介绍了.onCreate云函数触发后如何更新或设置Firestore文档的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试通过使用Cloud Functions for Firebase将Stripe与Firebase的Cloud Firestore数据库集成. onCreate 触发器正确发生,但我也希望它更新或设置一个名为"customer_id"的特定字段到我的Users集合中的正确文档中.我认为我的函数编写方式有些不完善,因为我对javascript的了解不是最丰富的.

I'm currently trying to integrate Stripe with my Firebase's Cloud Firestore db through using Cloud Functions for Firebase. The onCreate trigger is happening correctly but I also want it to update or set a specific field called "customer_id" into the right document in my Users collection. I think something is a little off about how I write my function since I'm not the most experienced with javascript.

我也尝试过

return admin.firestore().ref(`Users/${user.uid}/customer_id`).set(customer.id);

'use strict';

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
//const logging = require('@google-cloud/logging')();
const stripe = require('stripe')(functions.config().stripe.token);
const currency = functions.config().stripe.currency || 'USD';

// When a user is created, register them with Stripe
exports.createStripeCustomer = functions.auth.user().onCreate((user) => {
    return stripe.customers.create({
      email: user.email,
    }).then((customer) => {
        return admin.firestore().collection("Users").doc(user.uid).update({"customer_id": customer.id})
    });
  });

使用Stripe创建客户没有问题,但是Firestore数据库上的"customter_id"字段未得到更新.

Customer gets created with Stripe no problem but the "customter_id" field is not getting updated on the Firestore db.

数据库的打印屏幕:

错误日志的打印屏幕:

Print screen of the error log:

推荐答案

如评论中所述,在错误日志的打印屏幕中,您部署的代码与您在问题中引用的代码不对应

As said in the comments, from the print screen of the error log, the code you have deployed does not correspond to the code you are referencing to in your question.

您问题中的代码看起来正确.

The code in your question looks correct.

这篇关于.onCreate云函数触发后如何更新或设置Firestore文档的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆