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

家住魔仙堡 提交于 2021-01-28 07:34:33

问题


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:


回答1:


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.



来源:https://stackoverflow.com/questions/54497718/how-to-update-or-set-property-of-firestore-document-after-oncreate-cloud-functi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!