nodejs firebase error RangeError: Maximum call stack size exceeded failure
Asked Answered
P

2

15

I have an error from the firebase :

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

I didn't find my mistake.

I'm very lost in here, please help.

My code looks like this:

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);
    }
});
Philbo answered 26/1, 2017 at 18:59 Comment(0)
A
17

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.

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);
    }
Asha answered 26/1, 2017 at 19:36 Comment(1)
Thanks Frank, that helped me too, I was doing the same thing, was stuck since last 3 days.Implantation
D
2

I was facing the same error

Error:

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)

My callable function :

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

I solved it by changing node package version in functions/package.json from 8 to 10 and it's working fine now.

Duvall answered 28/4, 2020 at 15:33 Comment(1)
This resolved the issue, wasted a lot of time on this issueCasabonne

© 2022 - 2024 — McMap. All rights reserved.