简体   繁体   English

在 Firebase 中增加节点时出现“超出最大调用堆栈大小”

[英]Getting 'max call stack size exceeded' when incrementing a node in Firebase

I'm trying to increment my 'week' value in firebase by 1.我正在尝试将 firebase 中的“周”值增加 1。

One solution i've been trying was this:我一直在尝试的一种解决方案是:

var ref = db.ref();
    ref.on('value', function (data) {
        var week = data.val().week + 1;
        ref.update({
            week
        });
    });

but it throws a "max call stack size exceeded" error (Even though firebase still updates appropriately)但它会引发“超出最大调用堆栈大小”错误(即使 firebase 仍会适当更新)

I also tried我也试过

var ref = db.ref('week');
    ref.on('value', function (data) {
        var week = data.val();
        ref.set(week+1);
    });

this works as well, but it throws the same error这也有效,但会引发相同的错误

Can someone provide a solution that doesn't throw an error?有人可以提供一个不会引发错误的解决方案吗? thanks谢谢

The reason is because you're using on() to read the data.原因是因为您正在使用on()读取数据。 When you use on(), you're setting up a listener that gets invoked any time the data at the location changes.当您使用 on() 时,您正在设置一个侦听器,该侦听器会在该位置的数据发生变化时被调用。 So what you have written does this:所以你写的内容是这样的:

  1. Listen to the location 'week'收听位置“周”
  2. When the value is known, or when it has changed , get it from the snapshot当该值已知或已更改时,从快照中获取
  3. Increment the value by 1, and write it back to 'week'将值增加 1,并将其写回 'week'
  4. Since the value of 'week' has changed, goto step 2 so the listener can process the change.由于“week”的值已更改,请转到第 2 步,以便侦听器可以处理更改。

This effectively an infinite loop.这实际上是一个无限循环。

If you just want to increment a single time, use once() instead of on() to read data once .如果您只想增加一次,请使用once()而不是 on() 读取数据一次

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM