【发布时间】:2020-08-22 11:26:57
【问题描述】:
刚开始使用 Firebase 函数并让示例工作,但很困惑,因为如果我将“消息”集合更改为不同的名称(例如“列表”),更新事件不会发生。我在“add”和“makeUppercase”行的两个地方更改了“messages”这个词。我得到响应 OK,它将数据写入集合,但不触发事件。一定很简单,但不能google。
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.addMessage = functions.https.onRequest(async (req, res) => {
// Grab the location parameter.
const inputcode = req.query.code || 'blank';
// Push the new message into Cloud Firestore using the Firebase Admin SDK.
const writeResult = await admin.firestore().collection('messages').add({inputcode: inputcode});
// Send back a message that we've succesfully written the message
res.json({result: `Message with ID: ${writeResult.id} added.`});
});
exports.makeUppercase = functions.firestore.document('/messages/{documentId}')
.onCreate((snap, context) => {
// Grab the current value of what was written to Cloud Firestore.
const inputcode = snap.data().inputcode;
// Access the parameter `{documentId}` with `context.params`
functions.logger.log('Uppercasing', context.params.documentId, inputcode);
const areacode = inputcode.toUpperCase();
const written = new Date();
return snap.ref.set({written, areacode}, {merge: true});
});
顺便说一句,我正在使用本地 firebase 模拟器来做这个测试。
这是新版本,仅在两个地方将“消息”更改为“vvvv”。
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.addMessage = functions.https.onRequest(async (req, res) => {
// Grab the location parameter.
const inputcode = req.query.code || 'blank';
// Push the new message into Cloud Firestore using the Firebase Admin SDK.
const writeResult = await admin.firestore().collection('vvvvv').add({inputcode: inputcode});
// Send back a message that we've succesfully written the message
res.json({result: `Message with ID: ${writeResult.id} added.`});
});
exports.makeUppercase = functions.firestore.document('/vvvvv/{documentId}')
.onCreate((snap, context) => {
// Grab the current value of what was written to Cloud Firestore.
const inputcode = snap.data().inputcode;
// Access the parameter `{documentId}` with `context.params`
functions.logger.log('Uppercasing', context.params.documentId, inputcode);
const areacode = inputcode.toUpperCase();
const written = new Date();
return snap.ref.set({written, areacode}, {merge: true});
});
【问题讨论】:
-
您是说 Firestore 功能根本没有触发,并且功能日志中没有任何内容吗?你确定函数已经部署了吗?
-
如果我使用如图所示的代码,就会触发函数。例如,如果我将名称“消息”更改为“视图”,则没有事件触发器。除了 addMessage 的 Begin 和 FInshed 之外,日志中没有任何内容。
-
您为什么希望该函数针对不同集合中的文档运行?它明确设置为识别特定的路径模式。我建议编辑问题,以更清楚地了解无法按您期望的方式工作的具体情况。问题中应该有足够的信息,以便任何人都可以复制问题。
-
不,我只是想设置一个不同的集合名称。我希望函数能够运行,因为我正在执行该行,```firestore().collection('vvvvv').add ``` 然后在 ```functions.firestore.document('/vvvvv 上监听事件/{documentId}') .onCreate ``` 我改变的只是'messages' => 'vvvvv'。上面的代码有效,但我想更改名称“消息”。不知道如何更好地解释。
-
好的,所以,我建议编辑问题以显示新的修改后的代码,它不能按您期望的方式工作。确保在测试之前部署它。同样,我们应该能够从您的问题中提取代码,并亲眼看看它是如何工作的。
标签: javascript firebase google-cloud-firestore google-cloud-functions