Google cloud firestore 云函数上下文参数返回未定义

Google cloud firestore 云函数上下文参数返回未定义,google-cloud-firestore,google-cloud-functions,Google Cloud Firestore,Google Cloud Functions,我正在使用云函数监听Firestore上创建的新文档 functions.firestore.document('users/{userId}') .onCreate((snapshot, context) => { console.log('params', context.params.userId); }); 日志显示未定义的而不是通配符参数 这将从2018年12月15日午夜开始 这是否与firestore/cloud功能的更新有关? 我们如

我正在使用云函数监听Firestore上创建的新文档

functions.firestore.document('users/{userId}')
        .onCreate((snapshot, context) => {
            console.log('params', context.params.userId);
});
日志显示未定义的而不是通配符参数

这将从2018年12月15日午夜开始

这是否与firestore/cloud功能的更新有关?
我们如何绕过这个问题

Firebase Functions SDK或平台目前(2018年12月15日)似乎存在缺陷

解决方法:

更新访问父文档ID的正确方法是通过
更改.after.ref.parent.parent.ID
快照.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...
  });
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...
  });
对于
onWrite
触发功能:

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...
  });
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的方法。

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


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

我们昨晚也开始遇到这种情况。可能的解决方法是在快照对象中使用ID,例如
const userId=snapshot.ID现在有一个Github问题:似乎该问题已发布在Firebase的状态仪表板中:status.firebase.google.com/incident/Functions/18044谢谢。解决方案似乎正在运行,但我遇到了一个与我的代码无关的新问题:TypeError:无法读取Firestore.snapshot上未定义的属性“name”(/user\u code/node\u modules/firebase admin/node\u modules/@google cloud/Firestore/build/src/index.js:422:127)在cloudFunctionNewSignature的changeConstructor(/user\u code/node\u modules/firebase functions/lib/providers/firestore.js:140:30)处(/user\u code/node\u modules/firebase functions/lib/providers/firestore.js:144:49)(/user\u code/node\u modules/firebase functions/lib/cloud functions。我甚至无法想象有多少生产系统因此而中断……他们对此发表了任何声明吗?@iouhammi我们也看到了这个错误。现在正在寻找解决方法。@iouhammi我对示例进行了更新。
snapshot.ref.parent
显然是集合引用。你必须使用
snapshot.ref.parent.parent
来查找父文档。很高兴我找到了。今天我们这边的一切都因为这个而被破坏了。哪个SDK?firebase functions“NPM包?这是我们包的摘录。json:“``引擎”:{“节点”:“8”},“依赖项”:{“@google cloud/storage:“2.3.1”,“firebase admin:“^6.3.0”,“firebase functions:“^2.1.0”}`` firebase functions:2.1.0是我一直在问的问题。firebase functions似乎不再能够防止服务器的更改。我们正在继续回滚。好的,谢谢。仅供参考,我可以从我们的纱线中看到我们得到的.lock”@google cloud/firestore@^0.19.0“间接也是如此。我的依赖项:{“firebase admin”:“^6.0.0”,“firebase函数”:“^2.1.0”)。在package-lock.json中,我可以看到这个依赖项“@google cloud/firestore”:“0.16.1”“,与firebase管理员要求的版本相同。我也在使用Node 8。我的应用程序昨天几乎一整天都在关闭(IST)我一直紧盯着我的笔记本电脑,试图手动完成云功能所能做的一切,直到我不得不要求我的客户在过去的几个小时内停止使用该应用程序。这是一个新的应用程序,很少有客户使用我们进行日常计费,所以如果该应用程序停止工作,这是一个大问题。不过,该应用程序现在运行良好。谢谢你的帮助!有吗如果我们面临这些问题,有什么建议吗?