You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Firebase Cloud Functions V2 onDocumentWritten触发时出现TypeError: Cannot read properties of undefined (reading 'after')问题排查求助

Firebase Cloud Functions V2 onDocumentWritten触发时出现TypeError: Cannot read properties of undefined (reading 'after')问题排查求助

我刚部署了一个Node.js编写的Firebase云函数,可每次onDocumentWritten触发器触发后,控制台都会立刻抛出下面这个错误:

2023-12-31 12:05:53.028 WAT
TypeError: Cannot read properties of undefined (reading 'after')
2023-12-31 12:05:53.028 WAT
at /workspace/index.js:13:36
2023-12-31 12:05:53.028 WAT
at func (/workspace/node_modules/firebase-functions/lib/v2/providers/firestore.js:239:16)
2023-12-31 12:05:53.028 WAT
at /layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/function_wrappers.js:141:25
2023-12-31 12:05:53.028 WAT
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

我尝试打印event对象,得到的结果却是一个全是数字键的奇怪结构,完全不是预期的触发器事件格式:

{0: 10, 1000: 115, 1001: 101, 1002: 114, 1003: 95, 1004: 108, 1005: 97, 1006: 115, 1007: 116, 1008: 95, 1009: 108, 100: 83, 1010: 111, 1011: 10....... etc}

下面是我的函数代码:

const {logger} = require("firebase-functions");
const {onDocumentWritten} = require("firebase-functions/v2/firestore");
// The Firebase Admin SDK to access Firestore.
const {initializeApp} = require("firebase-admin/app");
const {getFirestore} = require("firebase-admin/firestore");

initializeApp();

// Trigger the function on user document change
exports.updateRecommendationsOnWrite = onDocumentWritten("users/{userId}",
(event) => {
  logger.log(event)
  const userDoc = event.data.data();
  // Ensure that the user document has user_interests
  const userInterests = userDoc.user_interests || [];

  // Retrieve all users' interests from the database
  const allUsersSnapshot = userDoc.ref.parent.get();
  const allUsersData = allUsersSnapshot.docs.map((doc) => doc.data());

  // Create a matrix of user interests (0: not interested, 1: interested)
  const interestsMatrix =
    allUsersData.map((userData) =>
      userData.user_interests.split(",")
        .map((interest) => userInterests.includes(interest.trim()) ? 1 : 0),
    );

  // Run collaborative filtering to get recommendations
  const recommendations =
    recommend.cFilter(interestsMatrix, allUsersData.length - 1);

  // Extract details of recommended users
  const recommendedUsers =
    recommendations.map((index) => allUsersData[index].user_id).filter(Boolean);

  logger.log("Recommendations updated successfully");

  // Update the user_recommendations field in the user document
  userDoc.ref.update({
    user_recommendations: recommendedUsers,
  });
});

我完全搞不懂为什么event对象会是这种奇怪的结构,没有触发器应该带的常规参数,有没有大佬能帮我排查下问题出在哪?

备注:内容来源于stack exchange,提问作者ERONDU EMMANUEL

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.20 12:53:10