When I try to add trigger for Firestore OnCreate the deployment fails for HTTP Error: 400, The request has errors.
Basically I want to write to a new doc if the record is created in different collection. but get the userId. this involves 2 write and 1 read. not sure if it is efficient way
But the log and the error has no detail on what the error is
Here's my code
'use strict';
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
const requestPromise = require('request-promise');
admin.initializeApp();
const firestore = admin.firestore();
const cors = require('cors')({
origin: true,
});
exports.joinCommunity = functions.region('europe-west2').firestore.document('users/{userId}/community')
.onCreate((snap, context) => {
const data = snap.data();
const communityRef = firestore.doc(`community/${data['cid']}`);
const user = {
uid: context.params.userId,
joined_on: context.timestamp
}
return communityRef.collection('members').doc(context.params.userId).set(user, { merge: true });
});
Appreciate the support or pointer
Solution: The problem was that I had used the Collection path instead of the document path. Changing the .document(users/{userId}/community') to .document('users/{userId}/community/{communityId}') fixed the error
Additionally: Also note in my code i have used Javascript way of export in typescript. Not an issue but the right way yo use it export const joinCommunity