nodejs firebase错误RangeError:超出最大调用堆栈大小的失败 [英] nodejs firebase error RangeError: Maximum call stack size exceeded failure
问题描述
我的firebase出现了错误:
I have an error from the firebase :
FIREBASE警告:用户回调引发了异常. RangeError:超出了最大调用堆栈大小
FIREBASE WARNING: Exception was thrown by user callback. RangeError: Maximum call stack size exceeded
我没有发现我的错误.
我在这里很迷路,请帮助.
I'm very lost in here, please help.
我的代码如下:
app.post('/updateCoords', (req, res)=>{
var usrID = req.body.id;
var usrCoords = {
lat: req.body.lat,
long: req.body.long
}
console.log('userID : '+usrID+' lat : '+usrCoords.lat+' long : '+usrCoords.long);
var ref = database.ref('users');
try{
ref.orderByChild('username').equalTo(usrID).on("value", (snapshot)=> {
if(!snapshot.val()){
// Error
return res.json({msg: 'username is not in D.B', success: false});
}
// Success
admin.database().ref('users/' + usrID + '/currentLocation').update({
lat: usrCoords.lat,
long: usrCoords.long
});
return res.json({msg: 'user coords changed', success: true});
});
}catch(ex){
console.log('ex /updateCoords = '+ex);
}
});
推荐答案
您正在更新要读取的同一节点.这将导致on("value"
回调再次被触发.然后依次写入一个新值,该值再次触发回调.这一直持续到运行时用完调用堆栈空间为止.
You're updating the same node that you're reading. This causes the on("value"
callback to be triggered again. Which in turn then writes a new value, which trigger the callback again. And that continues until the runtime runs out of call stack space.
最简单的解决方案是使用once()
代替on()
:
The simplest solution is to use once()
instead of on()
:
var ref = database.ref('users');
try{
ref.orderByChild('username').equalTo(usrID).once("value", (snapshot)=> {
if(!snapshot.val()){
checker = true;
}
if(snapshot.val()){
admin.database().ref('users/' + usrID + '/currentLocation').update({
lat: usrCoords.lat,
long: usrCoords.long
});
return res.json({msg: 'user coords changed', success: true});
// checker = false;
}
// res.json({msg: 'username is not in D.B', success: false});
});
}catch(ex){
console.log('ex /updateCoords = '+ex);
}
这篇关于nodejs firebase错误RangeError:超出最大调用堆栈大小的失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!