[英]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:
所以你写的内容是这样的:
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.