简体   繁体   English

.onCreate云函数触发后如何更新或设置Firestore文档的属性

[英]How to update or set property of firestore document after .onCreate cloud function trigger

I'm currently trying to integrate Stripe with my Firebase's Cloud Firestore db through using Cloud Functions for Firebase. 我目前正在尝试通过使用Cloud Functions for Firebase将Stripe与Firebase的Cloud Firestore数据库集成。 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. onCreate触发器发生正确,但我也希望它将其更新或设置一个名为“ customer_id”的特定字段到我的用户集合中的正确文档中。 I think something is a little off about how I write my function since I'm not the most experienced with javascript. 我认为关于函数的编写方式有些不足,因为我不是最有经验的javascript。

I've also tried 我也尝试过

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})
    });
  });

Customer gets created with Stripe no problem but the "customter_id" field is not getting updated on the Firestore db. 使用Stripe创建客户没有问题,但是Firestore数据库上的“ customter_id”字段未更新。

Print screen of the database: 数据库的打印屏幕: 在此处输入图片说明

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. 您问题中的代码看起来正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM