【问题标题】:Cloud Functions database event always contains empty dataCloud Functions 数据库事件始终包含空数据
【发布时间】:2017-10-08 04:26:15
【问题描述】:

我的云函数存在问题,我的所有数据库事件都返回空。例如,在以下事件中 event.data.val() 将返回 null。我正在执行更新操作,并通过使用 shell 以及部署后测试云功能来测试更新。

export const createSubscription = functions.database.ref('/users/{userId}/subscription').onWrite( event => {
  if(!event.data.val()) {
      return;
  }
});

但我可以轻松地挂接到如下所示的 auth.user() 事件并接收数据。

export const createStripeUser = functions.auth.user().onCreate(event => {
  const user = event.data;
});

编辑:将数据传递到集合中,例如模拟器控制台上的下面的那个

createSubscription({
   testKey: 'testValue'
})

或以下来自我的前端

db.ref(`/users/23213213213/subscription`).update({ testKey: 'testValue'}); 

将在函数上返回 null。

【问题讨论】:

  • 您能否编辑您的问题,以更具体地说明您正在执行的数据库写入应该将一些数据放入事件中,但没有?
  • @DougStevenson 刚刚更新了问题
  • 您仍然没有说明您正在写入您希望触发函数的数据库的内容和位置。
  • 我正在添加一个对象。让我再说明一下
  • 一件事 - 你没有为 onWrite 正确调用模拟器函数。它有两个参数,一个是之前的数据,另一个是新的数据。 myDatabaseFunction({before: 'old_data', after: 'new_data' }) firebase.google.com/docs/functions/local-emulator

标签: firebase firebase-realtime-database google-cloud-functions


【解决方案1】:

道格史蒂文森是正确的。对于 .onCreate(),您将使用 myDatabaseFunction('new_data') 正确地进行操作。

使用 .onWrite() 您需要像下面的示例一样传入前后。

如果我这样做了,你可能会被卡住。请注意,我在最终 JSON 之前和之后都有一个大括号。没有它们就无法正常工作。

addComment({before: {"comment":"before comment","role":"guest"}, after:{"comment":"After Comment","role":"guest"}})

希望我的示例比通用字符串参数有所帮助。

祝你好运!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-03
    • 1970-01-01
    • 2015-01-02
    • 2017-10-18
    • 1970-01-01
    • 1970-01-01
    • 2019-09-25
    • 2017-10-10
    相关资源
    最近更新 更多