Firestore trigger not working when run in emulator

Viewed 174

I'm new to Firebase and tried following this video.

code:

const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp()
const db = admin.firestore()
exports.onUserCreate = functions.firestore.document('users/{userId}').onCreate(async(snapshot, context) => {
const values = snapshot.data()
await db.collection('logging').add({description: `Email was sent to user with username:${values.username}`}) })

This should create a new collection called 'logging' whenever a new collection is created. But the trigger doesn't work in emulator, it shows only the collection which we created.

1 Answers

It looks like my comment was helpful, posting it as an answer for visibility.

A new collection is automatically created when you create a new document using the set() method.

There is no separate method for creating collections. Try adding a document as described in the docs. For example,

await db.collection('logging').doc('journal').set(data);
Related