4
votes

I am trying to to run a firebase cloud function to make an atomic (batch) update for an Angular 5 project, whenever a new customer is created.

I keep getting this error and cannot work out what the problem is:

console.error

Error: Cannot encode type ([object Undefined]) to a Firestore Value at 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();

    });
});
1

1 Answers

1
votes

I just spotted the error. I am defining currentTime and this.timestamp but I never defined timestamp.