Firestore触发器未在模拟器上运行

时间:2019-07-13 19:13:18

标签: firebase google-cloud-firestore google-cloud-functions firebase-cli

在本地Firestore和函数仿真器上运行我的Firestore触发器时,它们实际上并未触发。有时我会触发它们,但是随后我做了一些小改动,一切突然停止了工作。

我的仿真器在节点8上运行。与仿真器交互的代码在节点12上运行。我可以知道该仿真器正在工作,因为当我多次运行测试代码时,我可以看到正在添加文档。到集合中,但是我的onCreate触发器从未运行。

这是我的触发器:

import * as functions from 'firebase-functions';

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

这是我的测试代码:

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);
});

这是模拟器启动的输出:

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.

这是我的测试代码的输出:

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

这是我的Firestore日志:

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.

目前还不确定该怎么办。我不明白为什么触发器不触发。当我再次运行测试代码时,先前创建的字符ID也会被打印出来,因此Firestore显然正在运行并接受数据。

1 个答案:

答案 0 :(得分:0)

事实证明,即使您不使用通配符,也必须存在通配符,才能触发触发器。

更改

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

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

解决了问题