firebase / firebase-tools

The Firebase Command Line Tools
MIT License
4.03k stars 951 forks source link

Emulator functions with error when start #6433

Closed felipej26 closed 1 year ago

felipej26 commented 1 year ago

[REQUIRED] Environment info

firebase-tools: emulator

Platform: functions

[REQUIRED] Steps to reproduce

import {onCall, HttpsError} from "firebase-functions/v2/https";
import {logger} from "firebase-functions";
import {getFirestore, Timestamp} from "firebase-admin/firestore";
import {getDatabase} from "firebase-admin/database";
import {initializeApp} from "firebase-admin/app";

const matchesApp = initializeApp({
  databaseURL: "https://guess-the-flag-c4195-matches.firebaseio.com/",
}, "matches");

const queuesApp = initializeApp({
  databaseURL: "https://guess-the-flag-c4195-queues.firebaseio.com/",
}, "queues");

const firestore = getFirestore();

exports.addMessage = onCall(async (request) => {
  const text = request?.data?.text;
  const playerName = request?.data?.playerName;
  const date = Timestamp.now();

  const roomId = "1234";

  try {
    await getDatabase(matchesApp).ref(`/matches/${roomId}/messages/`).push({
      playerName,
      text,
      date,
    });
    logger.info("New Message written");
    return {
      playerName,
      text,
    };
  } catch (error: any) {
    throw new HttpsError("unknown", error.message, error);
  }
});

exports.addUserToMatchQueue = onCall(async (request) => {
  const userId = request?.data?.userId;
  const date = Timestamp.now();

  if (!userId) return;

  const ref = firestore.collection("matches").doc("queues");

  await ref.set({
    userId,
    date,
  });

  return getDatabase(queuesApp).ref(`/users/${userId}`).push({
    date,
  })
    .catch((error: any) => {
      throw new HttpsError("unknown", error.message, error);
    });
});

[REQUIRED] Expected behavior

I want to connect my functions with the realtime database and cloud firestore.

[REQUIRED] Actual behavior

When I run the command firebase emulators:start I get the message:

⬢ functions: Failed to load function definition from source: FirebaseError: Functions codebase could not be analyzed successfully. It may have a syntax or runtime error

firebase-debug.log:

firebase-debug.log

google-oss-bot commented 1 year ago

This issue does not seem to follow the issue template. Make sure you provide all the required information.

sanderschnydrig commented 1 year ago

I suddenly get the same error message since yesterday.

firebase-debug.log

I upgraded nvm to node 18 to try if that is working, but it didnt work either, normally im working on node 16.

aalej commented 1 year ago

Hey @felipej26, thanks for providing code snippets and debug logs. It looks like the cause of the error is because you didn’t initialize a default app, since matchesApp is an instance of an app with the name of “matches” and queuesApp is an instance of an app with the name of “queues”.

From what I can tell, you are accessing the Firestore service for the App matchesApp which has the name of “matches”. To do this, could you try changing:

const firestore = getFirestore();

To

const firestore = getFirestore(matchesApp);
aalej commented 1 year ago

Thanks @sanderschnydrig for sharing your debug logs. I’m thinking that the cause of the error in your case is that there is a runtime or syntax error with your code that is preventing the Firebase CLI from analyzing your functions.

Could you provide a sample code snippet where the issue is reproducible?