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

Posted

技术标签:

【中文标题】Firestore 触发器未在模拟器上运行【英文标题】:Firestore triggers not running on emulator 【发布时间】:2019-11-23 02:45:28 【问题描述】:

在本地 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】:

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

变化

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

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

解决问题

【讨论】:

以上是关于Firestore 触发器未在模拟器上运行的主要内容,如果未能解决你的问题,请参考以下文章

Xamarin.Android EditText事件未在设备上触发

iOS 9.3 touchesBegan/Ended 未在模拟器中触发

SqlDependency OnChange 事件未在 IIS 服务器上触发

Yii 框架 -> 我的插入前数据库未在 save() 上触发

如何从 Python 运行时云函数访问 Google Cloud Platform Firestore 触发器

Javascript onloadedmetadata 事件未在 iOS 设备上触发