firebase / firebase-functions

Firebase SDK for Cloud Functions
https://firebase.google.com/docs/functions/
MIT License
1.02k stars 201 forks source link

RangeError: Maximum call stack size exceeded - INTERNAL ERROR #1591

Closed bilal1031 closed 2 weeks ago

bilal1031 commented 1 month ago

Related issues

[REQUIRED] Version info

node: v20.12.2

firebase-functions: ^5.0.1

firebase-tools: 13.7.5

firebase-admin: ^12.3.1

[REQUIRED] Test case

const functions = require("firebase-functions");
const admin = require("firebase-admin");

const GetList = functions.https.onCall(async (data, context) => {
  try {
    const pageRef = data?.pageRef;
    const limit = data?.limit ?? 10;
    const itemsLimit = data?.itemsLimit ?? 3;
    const allowItemsFetch = data?.allowItemsFetch ?? false;

    // If there is no page reference, get the total count of users
    let count = null;
    if (pageRef == undefined) {
      count = await admin.firestore().collection("users").count().get();
      count = count.data().count;
    }

    const res =
      pageRef != undefined
        ? await admin
            .firestore()
            .collection("users")
            .limit(limit)
            .startAfter(pageRef)
            .get()
        : await admin.firestore().collection("users").limit(limit).get();

    if (res.empty) {
      return {
        error: true,
        message: "No users found.",
        data: [],
        totalCount: 0,
      };
    } else {
      let users = [];

      // Get all users
      for (const doc of res.docs) {
        users.push(doc.data());
      }

      // if allowItemsFetch is true, fetch items for each user
      if (allowItemsFetch) {
        const itemsPromises = users.map(async (user) => {
          const items = await admin
            .firestore()
            .collection("items")
            .where("userId", "==", user.id)
            .limit(itemsLimit)
            .get();

          user.items = [];

          if (!items.empty) {
            for (const doc of items.docs) {
              user.items.push(doc.data());
            }
          }

          const lastDoc = items.docs[items.docs.length - 1];
          user.itemRef = lastDoc.ref;
          return user;
        });

        // Wait for all items to be fetched
        await Promise.all(itemsPromises);
      }

      return {
        error: false,
        message: "Users found.",
        data: users,
        pageRef: res.docs[res.docs.length - 1].ref,
        totalCount: count,
      };
    }
  } catch (error) {
    return {
      error: true,
      message: error.message,
      data: [],
      pageRef: null,
      totalCount: 0,
    };
  }
});

module.exports = GetList;

[REQUIRED] Steps to reproduce

{
    "data": {
    "pageRef": null,
    "limit": 10,
    "allowItemsFetch": true
    }
}

[REQUIRED] Expected behavior

I have created a function to get a user list with pagination. I am getting the doc ref in the first call and sending that ref as payload on the next function call when I want the next data. The function should work normally (I have tested it on local machine and it works fine) and provide me with the returned data at the end of the call each function call with pagination. I think the issue seems to be in the encode function when trying to parse the data object as it points to in the stack-trace in the attached image.

[REQUIRED] Actual behavior

Whenever I try to make a test call to the deployed function I get an INTERNAL ERROR, the issues seem to be in the https.js in common.

image

Were you able to successfully deploy your functions?

Yes, I was able to successfully deploy it.

exaby73 commented 4 weeks ago

Hey @bilal1031. Could you provide a minimal code I can use to reproduce this? I'm having trouble exactly understanding the issue here since the logs don't give much information

google-oss-bot commented 2 weeks ago

Hey @bilal1031. We need more information to resolve this issue but there hasn't been an update in 7 weekdays. I'm marking the issue as stale and if there are no new updates in the next 3 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

google-oss-bot commented 2 weeks ago

Since there haven't been any recent updates here, I am going to close this issue.

@bilal1031 if you're still experiencing this problem and want to continue the discussion just leave a comment here and we are happy to re-open this.