Home > OS >  firebase Cloud Functions Error: RangeError: Maximum call stack size exceeded
firebase Cloud Functions Error: RangeError: Maximum call stack size exceeded

Time:06-30

I'm tring to get stock data from an api.

and I want it be updated at every 12:00 PM.

but when I tring to deploy this funtions, using

$firebase deploy --only functions

the error comes out.

Error: Failed to load function definition from source: Failed to generate manifest from function source: RangeError: Maximum call stack size exceeded

This is "index.ts" in funtions/src.

import * as functions from "firebase-functions";
import axios from "axios"
import { doc, getFirestore, setDoc, Timestamp } from 'firebase/firestore'
import { format } from 'date-fns'
import { initializeApp } from "firebase/app";

const firebaseConfig = {
    apiKey: "***********",
    authDomain: "****",
    projectId: "",
    storageBucket: "",
    messagingSenderId: "",
    appId: "",
    measurementId: ""
};

// Initialize Firebase
export const app = initializeApp(firebaseConfig);
export const db = getFirestore(app);

const API_KEY = "****************"

interface Wow {
    clpr: string
    srtnCd: string
}

const today = new Date();
const yesterday = today.setDate(today.getDate() - 1);
const basDt = format(yesterday, 'yyyyMMdd')

export const getStocks = functions
    .region('asia-northeast3')
    .pubsub.schedule('12 00 * * *')
    .timeZone('Asia/Seoul').onRun(async (context) => {
        await axios.get(`https://api.odcloud.kr/api/GetStockSecuritiesInfoService/v1/getStockPriceInfo?numOfRows=3000&resultType=json&basDt=${basDt}&serviceKey=${API_KEY}`)
            .then(response => {
                const wow: Wow[] = response.data.response.body.items.item
                wow.map((v, index) => {
                    setDoc(doc(db, 'KRX', v.srtnCd), { day: Timestamp.fromDate(today), price: v.clpr })
                })
                return null;
            })
    })

Another functions do work well . but if I include this one, It doesn't work.

What should I do?

CodePudding user response:

Because this is your functions\src\index.ts file, you should only be exporting Cloud Functions from it and nothing else.

So change:

// Initialize Firebase
export const app = initializeApp(firebaseConfig);
export const db = getFirestore(app);

to:

// Initialize Firebase
const app = initializeApp(firebaseConfig);
const db = getFirestore(app);

If you want to export an initialized Firebase SDK for use elsewhere, move it to its own file called functions\src\firebase.ts (or similar):

// firebase.ts
import { initializeApp } from "firebase-admin/app";
import { getFirestore } from "firebase-admin/firestore";

export const app = initializeApp(); // no arguments uses the default service account
export const db = getFirestore(app);

then use it like so:

import { app, db } from "./firebase.ts"

// NOTE: at time of writing, @google-cloud/firestore still uses the legacy namespaced syntax
db.doc("path/to/some/document") 
  .set({ /* data */ })
  .then(() => console.log('success'))
  .catch((err) => console.error('error', err));
  • Related