Firebase函数打字稿

时间:2018-07-08 14:50:26

标签: javascript typescript firebase google-cloud-functions

我具有基于https://firebase.google.com/docs/firestore/extend-with-functions编写的此功能。

但是我的IDE VCode中出现错误

任何建议将不胜感激

[ts] Argument of type '(snap: any, context: any) => Promise<void>' is not 
assignable to parameter of type '(event: Event<DeltaDocumentSnapshot>) => any'.

Firebase示例代码-Javascript

exports.createUser = functions.firestore
    .document('users/{userId}')
    .onCreate((snap, context) => {
      // Get an object representing the document
      // e.g. {'name': 'Marie', 'age': 66}
      const newValue = snap.data();

      // access a particular field as you would any JS property
      const name = newValue.name;

      // perform desired operations ...
    });

我的代码-打字稿

import * as functions from 'firebase-functions';
import { db } from './config';
import { createCustomer } from './helpers';

export const createStripeCustomer = functions.firestore.document('users/{userId}').onCreate((snap, context) => {
    const user = snap.data();
    const userRef = db.collection('users').doc(user.uid);
    if (user.newCustomer === true) {
        return createCustomer(user)
            .then(customer => {
                // Get a new write batch
                const batch = db.batch();

                // Update the User Profile with Stripe Customer Id
                batch.set(userRef, { accountId: customer.id }, { merge: true });

                // Create Organization
                const orgRef = db.collection('miCustomers').doc(customer.id);
                batch.set(orgRef, {
                    id: customer.id,
                    accountId: customer.id,
                    name: customer.email,
                    email: customer.email
                });

                // Commit the batch
                return batch.commit().then(function() {
                    // ...
                });
            })
            .catch(console.log);
    } else {
        return null;
    }
});

0 个答案:

没有答案