Alyan Qureshi
Alyan Qureshi

Reputation: 111

Firebase doesn't deploy my function, says it isn't a cloud function

This is my function, getAccountType

import { firebaseAdmin } from './index';

export const getAccountType = async (data: any, context: any) => {
    const uid = context.auth.uid;
    try{
        const snapshot = await firebaseAdmin.firestore().collection('users').doc(uid).get();
        if(snapshot.exists){
            const data = snapshot.data() as { accountType : 'youth' | 'prof', firstName : string, isProfileComplete : boolean};
            return data;
        }
        else{
            return { message : "User does not exist"};
        };
    } catch(e){
        return { message : `Failed to get user data ${e}` };
    };
};

And this is my index.ts file

import * as functions from "firebase-functions";
import * as admin from 'firebase-admin';
import { helloWorld } from "./helloWorld";
import { getAccountType } from "./getAccountType";


export const firebaseAdmin = admin.initializeApp();
exports.helloWorld = functions.https.onCall(helloWorld)
exports.getAccountType = functions.https.onCall(getAccountType)

And here is the error I receive

i  functions: preparing functions directory for uploading...

Error: Error occurred while parsing your function triggers. Please ensure that index.js only exports cloud functions.

The HelloWorld function deploys just fine, but for some reason firebase thinks getAccountType is not a cloud function

Upvotes: 2

Views: 297

Answers (1)

Donnald Cucharo
Donnald Cucharo

Reputation: 4126

I was able to deploy your code on Firebase Cloud Functions by making some adjustments. The error is saying that you can only export functions. You cannot export declarations such as const.

To fix the error, remove firebaseAdmin on index.ts.

index.ts

import * as functions from "firebase-functions";
import { getAccountType } from "./getAccountType";

exports.getAccountType = functions.https.onCall(getAccountType)

getAccountType.ts

import * as admin from 'firebase-admin'

export const getAccountType = async (data: any, context: any) => {
    // Your code logic
    ...
};

Upvotes: 2

Related Questions