Firebase / Firestore Cloud Functions

I am trying to run the firebase cloud function to create an atomic (batch) update for an Angular 5 project when a new client is created.

I keep getting this error and can't decide what the problem is:

console.error

Error: cannot encode type ([object Undefined]) into Firestore value in Function.encodeValue (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/src/document.js: 658: 11)

Cloud Functions (index.js)
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
const stripe = require('stripe')(functions.config().stripe.testkey);

// 1. Create Stripe User on sign up

exports.createStripeCustomer = functions.auth.user().onCreate(event => {
    // user auth data
    const user = event.data;

    // register Stripe user
    return stripe.customers.create({
        email: user.email
    })
    .then(customer => {
        const db = admin.firestore();
        const batch = db.batch();
        const currentTime = this.timestamp;
        const customerDoc = db.collection('customers').doc(customer.id);
        const userDoc = db.collection('users').doc(user.uid);

        batch.set(customerDoc, {userId: user.uid, timestamp: currentTime });
        batch.set(userDoc, {customerId: customer.id, timestamp: currentTime });

        return batch.commit();

    });
});
+4
source share
1 answer

I just noticed an error. I define currentTimeand this.timestamp, but I never determined timestamp.

+1
source

Source: https://habr.com/ru/post/1689865/


All Articles