title: Firebase Function: Unhandled error RangeError: Maximum call stack size exceeded
body: <p>I have the following callable function that gets some data from my database, then creates a PDF with that data using html-pdf, uploads that PDF to storage and finally returns the name of the file in storage.
  It works well in https form, but I want to convert it to a callable function, and for some reason I can't figure out, it crashes with this error: RangeError: maximum call stack size exceeded.</p>
  
  <p>I suspect it has something to do with the fact that html-pdf doesn't work with promises and instead uses an error/data callback. But I've tried to convert it into a promise without success.</p>
  
  <pre><code>export const createPdf = functions.https.onCall((data, context) =&gt; {
      const refId = data.refId;
      const companyId = data.companyId;
      const userId = context.auth.uid;
  
      return admin.database().ref('/references').child(companyId).child(refId).once('value', (snapshot) =&gt; {
          const filePath = '/references/' + refId + '/pdfs/' + refId + '.pdf';
          const localeId = snapshot.child('locale').val();
  
          return admin.database().ref('/tags').child(localeId).once('value', (tagsSnapshot) =&gt; {
              const jsLocaleId = localeId.replace(/_/, "-");
              const projectDate = moment().locale(jsLocaleId)
                  .year(snapshot.child('year').val())
                  .month(snapshot.child('month').val() - 1)
                  .date(15)
                  .format('MMMM YYYY');
  
              const tags = tagsSnapshot.val();
              const projectCategories = ...
              const pictures = snapshot.child('pictures').val();
  
              const pdfData = {
                  projectName: snapshot.child('projectName').val(),
                  surface: snapshot.child('surface').val(),
                  companyName: snapshot.child('companyName').val(),
                  date: projectDate,
                  newBuilding: snapshot.child('newBuilding').val(),
                  customerName: snapshot.child('customerName').val(),
                  categories: projectCategories,
                  address: snapshot.child('address').val().replace(/\n/g, '&lt;br&gt;'),
                  satellite: snapshot.child('satellite').val(),
                  pictures: !isNullOrUndefined(pictures) ? pictures.map((item) =&gt; {
                      return {url: item}
                  }) : []
              };
              console.log("data", pdfData);
              const options = {...};
  
              const localTemplate = path.join(os.tmpdir(), 'share.html');
              const localPDFFile = path.join(os.tmpdir(), 'share.pdf');
              const languageCode = localeId.split("_")[0];
  
              return admin.storage().bucket().file('/templates/share-' + languageCode + '.html').download({destination: localTemplate}).then(() =&gt; {
                  const source = fs.readFileSync(localTemplate, 'utf8');
                  const html = handlebars.compile(source)(pdfData);
                  pdf.create(html, options).toFile(localPDFFile, function (err, result) {
                      if (err) {
                          console.log(err);
                          throw new functions.https.HttpsError('internal', err.message);
                      }
  
                      return admin.storage().bucket().upload(localPDFFile, {
                          destination: filePath,
                          resumable: false,
                          metadata: {contentType: 'application/pdf'}
                      }).then((files) =&gt; {
                          console.log("files", files);
                          return files[0].getMetadata().then((metadata) =&gt; {
                              const name = metadata[0]["name"];
                              return {
                                  name: name
                              };
                          });
                      }).catch(error =&gt; {
                          console.error(error);
                          throw new functions.https.HttpsError('internal', "Could not upload PDF because " + error.message);
                      });
                  });
              }).catch((error) =&gt; {
                  console.error("Could not download template");
                  throw new functions.https.HttpsError('internal', error.message);
              });
          });
      });
  });
  </code></pre>