Firebase Unhandled error RangeError: Maximum call stack size exceeded

Solution 1:

The problem is similar to the one described here. You're returning a complex object generated by the Firebase API called a DocumentSnapshot. A snapshot it not itself raw JSON data to return to the client, and it contains circular references to other objects. Cloud Functions is stuck trying to serialize all of these objects. Instead, just return the raw JavaScript object of the data at the location of interest by calling val() on the snapshot:

return database
    .ref('/products/' + product)
    .orderByKey()
    .endAt(ts)
    .limitToLast(1)
    .once('value')             // once() returns a promise containing a snapshost
    .then(snapshot => {
        return snapshot.val()  // this is the raw JS object
    })

You generally don't use both the returned promise and the callback in the same call. It's easier to just use the promise.

Solution 2:

It is worth mentioning that this is not explicitly written in the Firebase doc; that you should ALWAYS stringify objects before an HTTP request. On the other end simply parse it. Removing JSON.stringify() and JSON.parse() will trigger the exact same error.

CLIENT SIDE:

auth.signInWithEmailAndPassword(username, password).then(async (snapShot) => {
    const getToken = await functions.httpsCallable('getToken')(JSON.stringify(snapShot));
    ....
});

SERVER SIDE:

exports.getToken = functions.https.onCall(async (data, context) => {
    console.log(JSON.parse(data));
    ...
});