I have deployed cloud function (Gen 2) that is supposed to be triggered each time a new document in “messages” collection is created in my Firestore DB. Than the “test” data field with hardcoded string is supposed to be added to this document. I can see that the function is actually triggered on new doc creation, but I am keep getting 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)
Cannot figure out what is the issue.
This is the code that is being executed:
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 });
});
of I was also trying to return this:
return event.data.ref.set({test}, {merge: true});
which is basically same thing. Both fail.
Btw, I followed firebase docs and makeuppercase
function gives me the same error.
From the docs:
// 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});
});
How can I fix this issue?
Anastasia Kurayshevich is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.