CodeCombustion001
CodeCombustion001

Reputation: 151

Firestore triggers not running on emulator

When running my firestore triggers on a local firestore and functions emulators, they do not actually trigger. At times I have gotten them to trigger, but then I make a few small changes and everything suddenly stop working.

My emulator is running on node 8. The code that interacts with the emulator is running on node 12. I can tell that the emulator is working because when I run my test code multiple times, I can see that documents are being added to the collection, but my onCreate trigger never runs.

Here is my trigger:

import * as functions from 'firebase-functions';

export const charactersOnCreate = functions.firestore.document('characters').onCreate(() => {
    console.log('triggered');
})

Here is my test code:

import * as dotenv from 'dotenv';
import * as admin from 'firebase-admin';
import * as path from 'path';
import { credential } from '../config';

dotenv.config({
    path: path.join(__dirname, '../../.env.test')
});

console.log(process.env.FIRESTORE_EMULATOR_HOST);

const app = admin.initializeApp({ credential });

const db = app.firestore();

const runTest = async () => {
    const charactersRef = db.collection('characters');
    const allCharacters = await charactersRef.get()
    allCharacters.forEach(character => {
        console.log('\t', character.id);
    });
    const userDoc = await db.collection('users').doc();
    console.log(userDoc.id);
    await userDoc.set({
        characters: []
    });
    const characterDoc = await db.collection('characters').doc();
    console.log(characterDoc.id);
    await characterDoc.set({
        ownerRef: userDoc
    });
    const userData = (await userDoc.get()).data();
    const characterData = (await characterDoc.get()).data();
    console.log(characterData);
    console.log(userData);
}

runTest().then(() => {
    console.log('done');
}).catch((err) => {
    console.error(err);
});

Here is the output of the emulator start up:

i  Starting emulators: ["functions","firestore"]
✔  functions: Using node@8 from host.
✔  functions: Emulator started at http://localhost:5001
i  firestore: Logging to firestore-debug.log
✔  firestore: Emulator started at http://localhost:8080
i  firestore: For testing set FIRESTORE_EMULATOR_HOST=localhost:8080
i  functions: Watching "/Users/josh/Development/Projects/naturoll-api/functions" for Cloud Functions...
i  functions: Setting up Cloud Firestore trigger "charactersOnCreate"
✔  functions: Trigger "charactersOnCreate" has been acknowledged by the Cloud Firestore emulator.
i  functions: Setting up Cloud Firestore trigger "charactersOnUpdate"
✔  functions: Trigger "charactersOnUpdate" has been acknowledged by the Cloud Firestore emulator.

Here is the output of my test code:

localhost:8080
2DUPXO9J6yrzbOuoDPzw
nfE7y8VhsoF5vxvHzHqA
{ ownerRef: 
   DocumentReference {
     _firestore: 
      Firestore {
        _settings: [Object],
        _settingsFrozen: true,
        _serializer: [Object],
        _projectId: 'naturoll-7b519',
        _lastSuccessfulRequest: 1563044750473,
        _preferTransactions: false,
        _clientPool: [Object] },
     _path: ResourcePath { segments: [Array] } } }
{ characters: [] }
done

Here is my firestore log:

API endpoint: http://localhost:8080
If you are using a library that supports the FIRESTORE_EMULATOR_HOST environment variable, run:

   export FIRESTORE_EMULATOR_HOST=localhost:8080

Dev App Server is now running.

Jul 13, 2019 3:05:39 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
INFO: Detected non-HTTP/2 connection.
Jul 13, 2019 3:05:39 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
INFO: Detected HTTP/2 connection.
Jul 13, 2019 3:05:40 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
INFO: Detected non-HTTP/2 connection.
Jul 13, 2019 3:05:50 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
INFO: Detected HTTP/2 connection.

I'm not sure what else to do at this point. I can't understand why the triggers aren't firing. When I run the test code again, the previously created character ids are printed out as well, so the firestore is clearly running and accepting data.

Upvotes: 7

Views: 4001

Answers (1)

CodeCombustion001
CodeCombustion001

Reputation: 151

It turns out, even if you don't use a wildcard, it needs to be present so the trigger fires.

Changing

export const charactersOnCreate = functions.firestore.document('characters').onCreate(() => {

to

export const charactersOnCreate = functions.firestore.document('characters/{characterId}').onCreate(() => {

fixes the issue

Upvotes: 5

Related Questions