nodejs firebase error RangeError: Maximum call stack size exceeded failure

nodejs firebase error RangeError: Maximum call stack size exceeded failure

我有一个来自 firebase 的错误:

FIREBASE WARNING: Exception was thrown by user callback. RangeError: Maximum call stack size exceeded

我没有发现我的错误。

我在这里很迷路,请帮忙。

我的代码如下所示:

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" 回调再次被触发。然后写入一个新值,再次触发回调。这一直持续到运行时耗尽调用堆栈 space。

最简单的解决方案是使用 once() 而不是 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);
    }

我遇到了同样的错误

错误:

Unhandled error RangeError: Maximum call stack size exceeded
    at Function.isNull (/srv/node_modules/lodash/lodash.js:11949:20)
    at encode (/srv/node_modules/firebase-functions/lib/providers/https.js:154:11)
    at /srv/node_modules/lodash/lodash.js:13401:38
    at /srv/node_modules/lodash/lodash.js:4905:15
    at baseForOwn (/srv/node_modules/lodash/lodash.js:2990:24)
    at Function.mapValues (/srv/node_modules/lodash/lodash.js:13400:7)
    at encode (/srv/node_modules/firebase-functions/lib/providers/https.js:179:18)
    at arrayMap (/srv/node_modules/lodash/lodash.js:639:23)
    at Function.map (/srv/node_modules/lodash/lodash.js:9554:14)
    at encode (/srv/node_modules/firebase-functions/lib/providers/https.js:173:18)

我的可调用函数:

exports.addRequest = functions.https.onCall((data, context) => {
    return admin.firestore().collection('requests').add({
        text: data.text,
        upvotes: 0,
    });
}); 

我通过将 functions/package.json 中的 node 包版本从 8 更改为 10 解决了这个问题,现在它工作正常。