Upon the creation of a new user, I would like to add data to a new collection in Firestore.
I have my functions set up as such;
exports.createUser = functions.firestore.document('Users/{userId}')
.onCreate((snap, context) => {
const newValue = snap.data();
if (snap.data() === null) return null;
const userRef = context.params.userId
console.log("create user found " + (userRef))
let notificationCollectionRef = firestoreDB.collection('Users').document(userRef).collection('Notifications')
let notificationDocumentRef = notificationCollectionRef.document('first notification')
return notificationDocumentRef.set({
notifications: "here is the notificiation"
}, {merge: true});
});
When running the function I get the console log correctly printing out the userId but I get the following error;
TypeError: firestoreDB.collection(...).document is not a function at exports.createUser.functions.firestore.document.onCreate (/user_code/index.js:23:73) at Object. (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:112:27) at next (native) at /user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71 at __awaiter (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12) at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:82:36) at /var/tmp/worker/worker.js:728:24 at process._tickDomainCallback (internal/process/next_tick.js:135:7)
I'm new to JS & Functions. As always any help greatly appreciated.
let notificationCollectionRef = snap.ref.collection('Notifications')
– James Poaglet firestoreDB = snap.ref.firestore
– James Poag