<code>functions.firestore.v2.document(...)</code>

问题描述 投票:0回答:0
const functions = require("firebase-functions"); const admin = require("firebase-admin"); const axios = require("axios"); const { FieldValue } = require('firebase-admin/firestore'); admin.initializeApp(); const db = admin.firestore(); exports.rahh = functions.firestore.v2.document("user/{userId}").onCreate(async (event) => { // ... (My function logic here - fetching data from Google People API) ... });

eRROR消息:


类型:无法读取未定义的属性(读取“文档”)

在对象。 (c:\ path o \ my unctions \ index.js:9:39) ...(其余的堆栈跟踪)...

问题:

尽管有正确的导入(

const functions = require("firebase-functions");
)并初始化firebase admin SDK(

admin.initializeApp();
),但功能对象上的firestore属性是未定义的,导致了错误。
我尝试过的是:

emulator重置:燃料模拟器:停止,燃料模拟器:清除,燃料模拟器:start -lyly函数

节点版本:切换到节点18(使用NVM)。

emulator运行:在模拟器上运行时,它可以很好地工作。

  • 包装锁定文件:已删除软件包 - 洛克(Lock-lock.json)和node_modules,然后npminstall

  • 全球燃烧箱CLI:NPM安装-G firebase -tools

    simplified Index.js:使用简单的HTTPS函数创建了一个最小索引。
  • togpack.json中的主要字段:正确将主字段设置为“ index.js”。
  • 为错别字进行了双重检查:已确认在导入或函数呼叫中没有错别字。
  • 使用基本的文本编辑器:在记事本中编辑的index.js来排除IDE问题。

  • package.json:

    { "name": "functions", "description": "Cloud Functions for Firebase", "scripts": { /* ... */ }, "engines": { "node": "18" }, "main": "index.js", "dependencies": { "axios": "^1.7.9", "firebase": "^11.2.0", // Firebase client SDK (might not be needed in functions) "firebase-admin": "^13.0.2", "firebase-functions": "^6.3.1" }, "devDependencies": { /* ... */ }, "private": true }
  • Firebase CLI版本:13.29.3
  • 一个新项目可以指向配置问题的事实,但我无法识别。

    即使使用正确的导入以及尝试上述所有故障排除步骤之后,可能会导致此“未定义”错误?
  • 您尝试使用V1云功能,但使用V1语法。

    定义v2触发器上的文档中,它显示了以下内容:
  • import { onDocumentWritten, onDocumentCreated, onDocumentUpdated, onDocumentDeleted, Change, FirestoreEvent } from "firebase-functions/v2/firestore"; exports.myfunction = onDocumentWritten("my-collection/{docId}", (event) => { /* ... */ });

    因此,您需要在那之后进行建模。
javascript node.js firebase google-cloud-functions
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.