skip to Main Content

I am trying to deploy my functions. I added a second one and refactored the first and now I can’t deploy. I tried deploying manually bypassing CI/CD but it still is giving the error. This is a 2.0 function and I did run tsc. Since I last deployed I did reinstall the firebase-tools package. Other than what I mentioned, nothing is different from when it did work.

Here are the output of firebase deploy --only functions:

i  deploying functions
Running command: npm --prefix "$RESOURCE_DIR" run lint

> lint
> eslint --ext .js,.ts .

Running command: npm --prefix "$RESOURCE_DIR" run build

> build
> tsc

✔  functions: Finished running predeploy script.
i  functions: preparing codebase default for deployment
i  functions: ensuring required API cloudfunctions.googleapis.com is enabled...
i  functions: ensuring required API cloudbuild.googleapis.com is enabled...
i  artifactregistry: ensuring required API artifactregistry.googleapis.com is enabled...
✔  functions: required API cloudbuild.googleapis.com is enabled
✔  functions: required API cloudfunctions.googleapis.com is enabled
✔  artifactregistry: required API artifactregistry.googleapis.com is enabled
i  functions: Loading and anaylzing source code for codebase default to determine what to deploy
Serving at port 8907

shutdown requested via /__/quitquitquit


Error: Functions codebase could not be analyzed successfully. It may have a syntax or runtime error

And my code:

/* eslint-disable max-len */
import "dotenv/config";
import { initializeApp } from "firebase-admin/app";
import { getFirestore } from "firebase-admin/firestore";
import { onDocumentCreated, onDocumentDeleted } from "firebase-functions/v2/firestore";

const accountSid = process.env.TWILIO_ACCOUNT_SID;
const authToken = process.env.TWILIO_AUTH_TOKEN;
const twilioPhoneNumber = process.env.TWILIO_PHONE_NUMBER;

// eslint-disable-next-line @typescript-eslint/no-var-requires
const client = require("twilio")(accountSid, authToken);

export const sendSMSOnContactWrite = onDocumentCreated("/contact/{contactSubmissionId}", async (event) => {
  const data = event.data?.data();
  console.log(data);
  return await client.messages.create({
    // eslint-disable-next-line
    "body": `New contact submission from ${data?.name} at ${data?.email} with message: ${data?.message}. ${data?.name} said ${data?.hasWebsite} when asked if they have a website.${data?.hasWebsite === "yes" ? " Their website is at " + data?.websiteAddress : ""}`,
    "from": twilioPhoneNumber,
    "to": process.env.MY_PHONE_NUMBER,
  });
});

export const sendSMSOnContentItemDelete = onDocumentDeleted("/websites/{websiteId}/content/{contentGroupId}/data/{contentId}", async (event) => {
  initializeApp();
  const db = getFirestore();
  const websiteDocRef = db.doc("websites/" + event.params.websiteId);
  const websiteDoc = await websiteDocRef.get();
  const websiteDocData = websiteDoc.data();
  if (!websiteDocData) {
    // TODO: Log error
    return;
  }
  const users = websiteDocData["users"];
  for (const userId of users) {
    const userDocRef = db.doc("users/" + userId);
    const userDoc = await userDocRef.get();
    const userDocData = userDoc.data();
    if (!userDocData) {
      // TODO: Log error
      return;
    }
    const deletionPreference = userDocData["preferences"]["deletion_sms"];
    if (deletionPreference) {
      const data = event.data?.data();
      console.log(data);
      return await client.messages.create({
        "body": `Content item with title ${data?.title} was deleted from website ${websiteDocData["url"]}`,
        "from": twilioPhoneNumber,
        "to": userDocData["phone_number"],
      });
    } else {
      return;
    }
  }
});

2

Answers


  1. We have found a solution and the cause: accessing function config params at deploy time.

    Apparently they can only be accessed at runtime, not deploy time. Moving those accesses inside the actual function code makes things work again.

    Login or Signup to reply.
  2. If you’re working with Cloud Functions of Gen 2:

    Check if you’re you’re using onRequest() Function correctly. I got the same error because i declared my function wrong.

    Do exports.functionName = onRequest((data, context) => { ...
    instead of exports.functionName = functions.onRequest((data, context) => { ...

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search