为什么Firebase事件在第二个及后续事件中返回空对象? [英] Why does Firebase event return empty object on second and subsequent events?

查看:55
本文介绍了为什么Firebase事件在第二个及后续事件中返回空对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在服务器上有一个Python Firebase SDK,可写入Firebase实时数据库.

I have a Python Firebase SDK on the server, which writes to Firebase real-time DB.

我在浏览器上有一个Javascript Firebase客户端,该客户端将自己注册为"child_added"事件的侦听器.

I have a Javascript Firebase client on the browser, which registers itself as a listener for "child_added" events.

身份验证由Python服务器处理.

Authentication is handled by the Python server.

使用Firebase规则允许读取,客户端侦听器将获取有关第一个事件的数据(该FB位置上的所有数据),但仅包含具有空数据的键,此后继子添加事件中的数据.

With Firebase rules allowing reads, the client listener gets data on the first event (all data at that FB location), but only a key with empty data on subsequent child_added events.

这是听众注册:

firebaseRef.on
    (
      "child_added",
      function(snapshot, prevChildKey)
      {
        console.log("FIREBASE REF: ", firebaseRef);
        console.log("FIREBASE KEY: ", snapshot.key);
        console.log("FIREBASE VALUE: ", snapshot.val());
      }
    );

"REF"总是很好.

"KEY"总是很好.

但是第一次完全检索该数据库位置后,"VALUE"为空.

But "VALUE" is empty after the first full retrieval of that db location.

我每次都在listen函数中尝试重新实例化Firebase引用.结果相同.

I tried instantiating the firebase reference each time anew inside the listen function. Same result.

我尝试了一个值"事件,而不是"child_added"事件.没有改善.

I tried a "value" event instead of "child_added". No improvement.

Firebase端的数据在FB控制台中看起来很完美.

The data on the Firebase side looks perfect in the FB console.

这是Python管理员将数据写入Firebase的方式:

Here's how the data is being written by the Python admin to firebase:

def push_value(rootAddr, childAddr, data):
  try:
    ref = db.reference(rootAddr)
    posts_ref = ref.child(childAddr)
    new_post_ref = posts_ref.push()
    new_post_ref.set(data)
  except Exception:
    raise

正如我所说,这非常适合将数据放置在FB中的正确位置.

And as I said, this works perfectly to put the data at the correct place in FB.

为什么在第一次下载数据库后,空事件对象发生后续事件?

Why the empty event objects after the first download of the database, on subsequent events?

推荐答案

我找到了答案.像大多数事情一样,事实证明这很简单,但是却花了几天的时间才找到.也许这可以拯救别人.

I found the answer. Like most things, it turned out to be simple, but took a couple of days to find. Maybe this will save someone else.

在文档页面上: http://firebase.google.com/docs/data/admin/save-data#section-push

在JavaScript和Python中,先调用push()然后 立即调用set()很常见,Firebase SDK可以让您 通过传递直接设置为push()的数据来组合它们 跟随..."

"In JavaScript and Python, the pattern of calling push() and then immediately calling set() is so common that the Firebase SDK lets you combine them by passing the data to be set directly to push() as follows..."

我建议措辞应强调您必须这样做.

I suggest the wording should emphasize that you must do it that way.

同一页面上的早期Python示例不起作用:

The earlier Python example on the same page doesn't work:

new_post_ref = posts_ref.push()
new_post_ref.set({
    'author': 'gracehop',
    'title': 'Announcing COBOL, a New Programming Language'
})

在此示例中,单独的空push()后跟set(data),不适用于Python和Javascript,因为在这种情况下,push()隐式也执行set(),因此,空的push会触发不必要的操作具有空数据的事件侦听器,并且set(data)也不触发具有数据的事件.

A separate empty push() followed by set(data) as in this example, won't work for Python and Javascript because in those cases the push() implicitly also does a set() and so an empty push triggers unwanted event listeners with empty data, and the set(data) didn't trigger an event with data, either.

换句话说,问题中的代码:

In other words, the code in the question:

new_post_ref = posts_ref.push()
new_post_ref.set(data)

必须是:

new_post_ref = posts_ref.push(data)

未明确调用set()的情况.

with set() not explicitly called.

由于此push()代码仅在将新对象写入FB时发生,因此不影响最初下载到客户端的操作.

Since this push() code happens only when new objects are written to FB, the initial download to the client wasn't affected.

尽管文档可能试图传达设计的演变,但未能指出只有给出的最后一个Python和Javascript示例可以使用,而其他示例则不能使用.

Though the documentation may be trying to convey the evolution of the design, it fails to point out that only the last Python and Javascript example given will work and the others shouldn't be used.

这篇关于为什么Firebase事件在第二个及后续事件中返回空对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆