【问题标题】:Cloud functions context params return undefined云函数上下文参数返回未定义
【发布时间】:2019-05-16 10:58:42
【问题描述】:

我正在使用云功能来监听在 Firestore 上创建的新文档。

functions.firestore.document('users/{userId}')
        .onCreate((snapshot, context) => {
            console.log('params', context.params.userId);
});

日志显示 undefined 而不是通配符参数。

从 2018 年 12 月 15 日午夜开始。

这是与 Firestore/云功能更新有关的错误吗? 以及我们如何绕过这个问题?

【问题讨论】:

  • 昨晚我们也开始遇到这种情况。可能的解决方法是使用快照对象中的 ID,例如const userId = snapshot.id;
  • 现在有一个 Github 问题:github.com/firebase/firebase-functions/issues/357
  • 似乎此问题已发布在 Firebase 的状态仪表板中。 : status.firebase.google.com/incident/Functions/18044

标签: google-cloud-firestore google-cloud-functions


【解决方案1】:

Firebase Functions SDK 或平台当前(2018 年 12 月 15 日)似乎存在错误。

解决方法:

更新 访问父文档 ID 的正确方法是通过 change.after.ref.parent.parent.idsnapshot.ref.parent.parent.id。请注意 .parent.parent

如果您期望带有文档 ID 的参数,您可以通过使用函数的第一个参数中提供的数据来解决此问题。

这是一个带有onCreate 触发函数的示例:

export const myCreateTriggeredFn = firestore
  .document("user/{userId}/friends/{friendId}")
  .onCreate((snapshot, context) => {

    let { userId, friendId } = context.params;

    if (typeof userId !== "string" || typeof friendId !== "string") {
      console.warn(`Invalid params, expected 'userId' and 'friendId'`, context.params);

      userId = snapshot.ref.parent.parent.id;
      friendId = snapshot.id;
    }

    // Continue your logic here...
  });

对于onWrite 触发函数:

export const myChangeTriggeredFn = firestore
  .document("user/{userId}/friends/{friendId}")
  .onWrite((change, context) => {

    let { userId, friendId } = context.params;

    if (typeof userId !== "string" || typeof friendId !== "string") {
      console.warn(`Invalid params, expected 'userId' and 'friendId'`, context.params);

      userId = change.after.ref.parent.parent.id;
      friendId = change.after.id;
    }

    // Continue your logic here...
  });

为了完整起见并突出显示错误,两个示例都显示了您通常如何从 context.params 中提取 ID,然后添加了从快照/更改对象中提取 ID 的解决方法。

【讨论】:

  • 谢谢。解决方法似乎有效,但我遇到了一个与我的代码无关的新问题:TypeError: Cannot read property 'name' of undefined at Firestore.snapshot_ (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/ firestore/build/src/index.js:422:127) 在 beforeSnapshotConstructor (/user_code/node_modules/firebase-functions/lib/providers/firestore.js:140:30) 在 changeConstructor (/user_code/node_modules/firebase-functions/ lib/providers/firestore.js:144:49) 在 cloudFunctionNewSignature (/user_code/node_modules/firebase-functions/lib/cloud-functions.
  • 我什至无法想象有多少生产系统因此而被破坏......他们有没有发布任何公告?
  • @iouhammi 我们也看到了这个错误。现在正在寻找解决方法。
  • @iouhammi 我对示例进行了更新。 snapshot.ref.parent 显然是集合参考。您必须使用snapshot.ref.parent.parent 来查找父文档。
  • 很高兴我找到了。因此,今天我们这边的一切都被打破了。
【解决方案2】:

我是处理此事件的 Google 员工。当 Firestore 处于私有 Alpha 版且尚未升级时,使用 SDK 的客户存在一个已知的兼容性问题。

使用低于 0.6.2 的 SDK 版本更新运行其代码的受影响客户是否会做出回应?如果您运行的是 0.6.1 版,则可以升级到 0.6.2,无需修改代码。

【讨论】:

  • 哪个 SDK? “firebase-functions”NPM 包?这是我们 package.json 的摘录:``` "engines": { "node": "8" }, "dependencies": { "@google-cloud/storage": "2.3.1", "firebase-管理员”:“^6.3.0”,“firebase-functions”:“^2.1.0”}```
  • firebase-functions: 2.1.0 是我要问的。似乎 firebase-functions 可能不再防范此服务器更改。我们将继续回滚。
  • 好的,谢谢。仅供参考,我可以在我们的 yarn.lock 中看到我们也间接获得了“@google-cloud/firestore@^0.19.0”。
  • 我的依赖项:{"firebase-admin": "^6.0.0", "firebase-functions": "^2.1.0"}。在 package-lock.json 我可以看到这个依赖 "@google-cloud/firestore": "0.16.1",与 firebase-admin 所需的版本相同。
  • 我也在使用 Node 8。昨天(IST)我的应用程序几乎一整天都处于关闭状态,我一直盯着我的笔记本电脑,试图手动执行云功能会做的所有事情,直到我不得不要求我的客户在过去几个小时内停止使用该应用程序。这是一个新的应用程序,很少有客户使用我们进行日常计费,所以如果应用程序停止工作,这是一个大问题。应用程序现在运行良好。谢谢您的帮助!如果我们遇到这类问题,有什么建议吗?
猜你喜欢
  • 2018-03-28
  • 1970-01-01
  • 1970-01-01
  • 2018-10-18
  • 2021-07-09
  • 1970-01-01
  • 1970-01-01
  • 2019-11-06
  • 2018-06-12
相关资源
最近更新 更多