OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Cloud function triggered by Firestore event

  • Thread starter Thread starter Ana
  • Start date Start date
A

Ana

Guest
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:

Code:
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:

Code:
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:

Code:
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:

Code:
// 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?
<p>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:</p>
<pre><code>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)
</code></pre>
<p>Cannot figure out what is the issue.</p>
<p>This is the code that is being executed:</p>
<pre><code>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 });
});
</code></pre>
<p>of I was also trying to return this:</p>
<pre><code>return event.data.ref.set({test}, {merge: true});
</code></pre>
<p>which is basically same thing. Both fail.</p>
<p>Btw, I followed firebase <a href="https://firebase.google.com/docs/functions/get-started?gen=2nd" rel="nofollow noreferrer">docs</a> and <code>makeuppercase</code> function gives me the same error.</p>
<p>From the docs:</p>
<pre><code>// 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});
});
</code></pre>
<p>How can I fix this issue?</p>
Continue reading...
 
Top