e.iluf
e.iluf

Reputation: 1659

firebase function Unhandled error RangeError: Maximum call stack size exceeded

i have been stuck on this error for sometime and still cant figure out the source. I am calling firebase function from client(javascript) but the firebase function side is throwing Maximum call stack error. Here is my code

exports.signInCart = functions.https.onCall(async (data, context) => {
    console.log(data)
    const sessionID = data.sessionID
     
    console.log("session id ", sessionID)
     
    const shopIntentRef = admin.database().ref('/shopIntent/'+ sessionID)

    try{
        shopIntentRef.once("value", (snapshot)=> {
            if(!snapshot.val()){
                console.log("not recognized ")
                
                return "NA"
            }

            if(snapshot.val()){
                 admin.database().ref('/shopintent/'+ sessionID+'/').update(data);

                return (snapshot.val());
            
            }
 
        });
    }catch(ex){
         console.log('ex /updateCoords = '+ex);
    }

   return shopIntentRef.once("value")
})

I tried couple of different things; i thought i should be able to get some value without the last return statement and I did get a return status code 200 but no snapshot.val() was returned. When I added the last return statement It started generating this error in firebase function console

Unhandled error RangeError: Maximum call stack size exceeded
at Object (<anonymous>)
at /workspace/node_modules/lodash/lodash.js:1198:19
at baseKeys (/workspace/node_modules/lodash/lodash.js:3484:16)
at keys (/workspace/node_modules/lodash/lodash.js:13333:60)
at /workspace/node_modules/lodash/lodash.js:4920:21
at baseForOwn (/workspace/node_modules/lodash/lodash.js:2990:24)
at Function.mapValues (/workspace/node_modules/lodash/lodash.js:13426:7)
at encode (/workspace/node_modules/firebase-functions/lib/providers/https.js:184:18)
at /workspace/node_modules/lodash/lodash.js:13427:38
at /workspace/node_modules/lodash/lodash.js:4925:15 

5:41:07.909 PM

how do I resolve the above errors?

Upvotes: 1

Views: 1573

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317948

You are trying to return a DataSnapshot object to the caller. Unfortunately, that object has circular references in it, and can't be serialized simply. You will need to get a plain JavaScript object out of it and return that instead.

return shopIntentRef.once("value").then(snapshot => {
    return snapshot.val()
})

Also, you will need to take some time to correctly handle the promises earlier in your code, otherwise it might not work the way you expect. Your code should only return a promise that resolves only after all the other async work is complete.

Upvotes: 1

Related Questions