Cloud function triggered by Firestore event

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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>
<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>
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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>
<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>
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>return event.data.ref.set({test}, {merge: true});
</code>
<code>return event.data.ref.set({test}, {merge: true}); </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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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>
<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>
// 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?

New contributor

Anastasia Kurayshevich is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật