Firebase update()不是带有查询的函数 [英] Firebase update() not a function with a query

查看:52
本文介绍了Firebase update()不是带有查询的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用AngularJS更新Firebase数据库中记录中的属性.我可以设置查询以查找我的记录:

I'm trying to update a property in a record in Firebase Database, with AngularJS. I can set up a query to find my record:

  firebase.database().ref('en/').orderByChild('word').equalTo('the').once('value')
  .then(function(snapshot) {
    snapshot.forEach(function(childSnapshot) {
      console.log(childSnapshot.val())
    });
  })

如果我对记录的密钥进行硬编码,我可以更新我的媒体资源:

I can update my property, if I hardcode in the record's key:

firebase.database().ref('en/-KloeQHDC-mugPjJMAG4').update({ wordFrequency: 111 })

但是,如果我设置查询以查找记录然后对其进行更新,则会收到错误消息update is not a function:

But if I set up a query to find the record and then update it, I get an error message update is not a function:

firebase.database().ref('en/').orderByChild('word').equalTo('the').update({ wordFrequency: 9001 })

另一个 answer 建议从forEach循环内部调用update():

Another answer suggests calling update() from inside a forEach loop:

  firebase.database().ref('en/').orderByChild('word').equalTo('the').once('value')
  .then(function(snapshot) {
    snapshot.forEach(function(childSnapshot) {
      console.log(childSnapshot.val()); // this works
      childSnapshot.ref().update({ wordFrequency: 9001 });
    });
  });

这将返回错误消息TypeError: childSnapshot.ref is not a function.我看不到childSnapshot是Firebase参考.

That returns an error message TypeError: childSnapshot.ref is not a function. I don't see how childSnapshot is a Firebase ref.

另一个 answer

当您在某个位置调用update()时,Firebase会遍历数据 您传入的情况(以asJson为例),并且对于每个键都执行一个 ref.child(key).set(value).

When you call update() on a location, Firebase loops over the data that you pass in (in your case asJson) and for each key performs a ref.child(key).set(value).

如果update()遍历数据,为什么我应该从forEach循环内部调用update()? 文档未显示正在调用update()forEach循环内部.

If update() loops over the data, why should I call update() from inside a forEach loop? The documentation doesn't show calling update() from inside a forEach loop.

推荐答案

Firebase数据库SDK提供了Reference.update()方法来更新数据库中单个位置的数据.这里的关键是Reference是数据库中的单个位置,因此很清楚要更新的内容.

The Firebase Database SDK provides a Reference.update() method to update data in a single location in a database. Key here is that a Reference is a single location in the database, so it is clear what to update.

我对多路径更新的工作方式的伪代码解释适用于数据库服务器如何实现它:给定一个位置/DatabaseReference,它将基于此位置更新update()调用中的每个路径.

My pseudo-code explanation about how multi-path updates work applies to how the database server implements it: given a single location/DatabaseReference it updates each path in the update() call based on that.

一个Query可以匹配数据库中的多个位置,因此它没有update()方法(或者set()remove()).

A Query can match multiple locations in the database, so it doesn't have an update() method (or set() or remove() for that matter).

要更新与查询匹配的每个位置,请执行查询,然后对每个结果调用update()-通过child_added侦听器或通过value侦听器以及上一个代码段中的循环.

To update each location matched by a query, you execute the query and then call update() on each result - either by a child_added listener, or with a value listener and a loop like in your last snippet.

这篇关于Firebase update()不是带有查询的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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