Reputation: 373
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
Upvotes: 0
Views: 470
Reputation: 83093
To define a Cloud Firestore trigger, you need to specify a document path. With users/{userId}/community
you are specifying a collection path.
So either you trigger your CF when a document is created in the users
collection (with users/{userId}
) or in the community
subcollection (with users/{userId}/community/{docId}
).
Have also a look at the following GitHub page https://github.com/firebase/functions-samples/tree/master/typescript-getting-started and in particular at the different ways (between JavaScript and TypeScript) for importing the Cloud Functions SDK and Admin SDK modules and for exporting the Cloud Functions.
Upvotes: 3