我已经部署了云函数(第 2 代),每次在我的 Firestore 数据库中创建“消息”集合中的新文档时,都会触发该函数。应该将带有硬编码字符串的“测试”数据字段添加到此文档中。我可以看到该函数实际上是在创建新文档时触发的,但我不断收到 TypeError:
TypeError: Cannot read properties of undefined (reading 'value')
at func (/workspace/node_modules/firebase-functions/lib/v2/providers/firestore.js:297:115)
at /layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/function_wrappers.js:113:25
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
无法弄清楚问题所在。
这是正在执行的代码:
const {logger} = require("firebase-functions");
const {onDocumentCreated} = require("firebase-functions/v2/firestore");
const {initializeApp} = require("firebase-admin/app");
const { getFirestore } = require("firebase-admin/firestore");
initializeApp();
const functionsRegion = "europe-west1";
exports.testtrigger = onDocumentCreated({region: functionsRegion}, "/messages/{documentId}", (event) => {
logger.log("Inside test_trigger function");
const test = "Function was triggered on new document creation.";
const documentRef = getFirestore().doc(event.data.value.name);
return documentRef.set({ test }, { merge: true });
});
我也试图退货:
return event.data.ref.set({test}, {merge: true});
这基本上是一样的事情。都失败了。
顺便说一句,我遵循 firebase docs 和
makeuppercase
函数给了我同样的错误。
来自文档:
// Listens for new messages added to /messages/:documentId/original
// and saves an uppercased version of the message
// to /messages/:documentId/uppercase
exports.makeuppercase = onDocumentCreated("/messages/{documentId}", (event) => {
// Grab the current value of what was written to Firestore.
const original = event.data.data().original;
// Access the parameter `{documentId}` with `event.params`
logger.log("Uppercasing", event.params.documentId, original);
const uppercase = original.toUpperCase();
// You must return a Promise when performing
// asynchronous tasks inside a function
// such as writing to Firestore.
// Setting an 'uppercase' field in Firestore document returns a Promise.
return event.data.ref.set({uppercase}, {merge: true});
});
如何解决这个问题?
好吧,在彻底阅读错误消息和文档后,我发现了问题 - 这就是我在该地区通过的方式。
错误消息指向库中的代码:/workspace/node_modules/firebase-functions/lib/v2/providers/firestore.js:297:115
这就是给出错误的函数:
function onOperation(eventType, documentOrOpts, handler) {
const { document, database, namespace, opts } = getOpts(documentOrOpts);
// wrap the handler
const func = (raw) => {
const event = raw;
const documentPattern = new path_pattern_1.PathPattern(typeof document === "string" ? document : document.value());
const params = makeParams(event.document, documentPattern);
const firestoreEvent = makeFirestoreEvent(eventType, event, params);
return (0, trace_1.wrapTraceContext)((0, onInit_1.withInit)(handler))(firestoreEvent);
};
func.run = handler;
func.__endpoint = makeEndpoint(eventType, opts, document, database, namespace);
return func;
}
exports.onOperation = onOperation;
特别是这一行:
new path_pattern_1.PathPattern(typeof document === "string" ? document : document.value())
这让我意识到该文件没有被正确通过。
解决方案:
我搜索了文档并找到了如何正确通过该区域:
const functionsRegion = "europe-west1"; //pass your region here
exports.tester = onDocumentCreated(
{
document: "messages/{documentId}",
region: functionsRegion,
},
"collection_name/{documentId}",
(event) => {
// rest of your code
});
现在一切正常。案件已结:)