0
votes

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.

2
let notificationCollectionRef = snap.ref.collection('Notifications')James Poag
Alternatively... let firestoreDB = snap.ref.firestoreJames Poag
@JamesPoag Getting the same error.David Lintin

2 Answers

2
votes

firestoreDB.collection('Users') returns a CollectionReference object. You're trying to call a method on that called document(), but as you can see from the API docs, there is no such method. I imagine you intend to use doc() instead to build a DocumentReference.

    let notificationCollectionRef = firestoreDB.collection('Users').doc(userRef).collection('Notifications')
    let notificationDocumentRef = notificationCollectionRef.doc('first notification')
2
votes

Here is the full working code:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firestore);

const firestoreDB = admin.firestore()

// Create and Deploy Your First Cloud Functions
// https://firebase.google.com/docs/functions/write-firebase-functions

exports.helloWorld = functions.https.onRequest((request, response) => {
 response.send("Hello from Firebase Cloud Functions!");
 console.log("function triggered")
});

exports.createUser = functions.firestore.document('Users/{userId}')
    .onCreate((snap, context) => {
        const newValue = snap.data();
        if (snap.data() === null) return null;
        const uid = context.params.userId
        let notificationCollectionRef = firestoreDB.collection('Users').doc(uid).collection('Notifications')
            return notificationCollectionRef.add({
                notification: 'Hello Notification',
                notificationType: 'Welcome'
            }).then(ref => {
               return console.log('notification successful', ref.id)
            })
        });