简体   繁体   中英

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. 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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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