0

Hi I want to serve only the functions of my firebase project; when I launch firebase serve --only functions on my prompt I get:

i  functions: Watching "/home/giuseppe/Documenti/projects/addlance/genial- 
lotto/functions" for Cloud Functions...
✔  functions: Using node@14 from host.
i  functions: Loaded environment variables from .env, .env.genial-lotto.
⬢  functions: Failed to load function definition from source: FirebaseError: Failed 
to load function definition from source: Failed to generate manifest from function 
source: TypeError: Cannot read property 'key' of undefined

this is my index.ts:

  import * as functions from "firebase-functions";
  import * as admin from "firebase-admin";

  import {
addUserProfile,
getUserProfile,
updateUserProfile,
} from "./insertUserProfile";
if (admin.apps.length === 0) {
admin.initializeApp();
}

exports.adminAddUserProfile = functions.https.onCall((data)=>{
addUserProfile(data);
});

exports.addCustomClaim = functions.https.onCall((data) => {
return admin.auth().getUserByEmail(data.email).then((user) => {
  return admin.auth().setCustomUserClaims(user.uid, data.claims).
      then(() => {
        return {
          message: ` Success!claims ${Object.keys(data.claim)} 
      have been set on ${data.email}`,
        };
      }).catch((err) => {
        return {error: err};
      });
});
});

exports.deleteAuthUser = functions.https.onCall((data)=>{
return admin.auth().getUserByEmail(data.email).then((user)=>{
  return admin.auth().deleteUser(user.uid).
      then(()=> {
        return {message: `Success! user ${data.email} has been deleted!`};
      }).
      catch((err)=>{
        return {error: err};
      });
});
});

exports.getUserProfile =functions.https.onCall((data)=>{
return getUserProfile(data);
});
exports.updateUserProfile= functions.https.onCall((data)=>{
return updateUserProfile(data);
}
);

this is insertUserProfile.ts:

    import {UserRecord} from "firebase-functions/v1/auth";
import {realtime} from "./configs/firebase";

const insertUser = async (user: UserRecord)=>{
  const ref = realtime.ref("userProfile");
  ref.push({key: user.uid,
    email: user.email,
  });
};
const addUserProfile = async (data: Record<string, never>) => {
  const ref = realtime.ref("userProfile");
  try {
    ref.push(data).then(()=>{
      console.log("done", data);
      return {"user": data};
    });
    return {status: 200,
      message: " userProfile added correctly"};
  } catch (error: unknown) {
    return {status: 500,
      message: error};
  }
};

const getUserProfile = async (data: {email: string})=>{
  const ref = realtime.ref("userProfile");
  const profilesList: any[]=[];
  const profiles = await ref.once("value");
  profiles.forEach((user)=>{
    profilesList.push(user?{...user.val(), key: user?.key}:null);
  }
  );
  return {
    status: 200,
    profile: JSON.stringify(profilesList.
        filter((prof)=>prof.email==data.email)[0]),
  };
};
const updateUserProfile = async (data: any)=>{
  const ref = realtime.ref(`userProfile/${data?.key}`);
  ref.update(data["userProfile"]);
  return {
    status: 200,
    message: "userProfile updated correctly",
  };
};
export {insertUser, addUserProfile, updateUserProfile, getUserProfile};

and this is how I initilize the app in functions/src/configs/firebase.ts:

    import * as admin from "firebase-admin";
import * as functions from "firebase-functions";

admin.initializeApp({
  credential: admin.credential.cert({
    privateKey: functions.config().private?.key.replace(/\\n/g, "\n"),
    projectId: functions.config().project.id,
    clientEmail: functions.config().client.email,
  }),
  databaseURL: "****",
  
});

const db = admin.firestore();
const realtime = admin.database();
export {admin, db, realtime};
arpho
  • 1,576
  • 10
  • 37
  • 57
  • https://stackoverflow.com/questions/56618725/firebase-serve-only-functions-vs-local-emulator-to-run-cloud-functions-locally – Wesley LeMahieu Feb 24 '23 at 23:44
  • Try deleting `.firebase` folder and run `firebase init`. And try to run `firebase deploy --only functions` instead of `firebase serve --only functions`. – Rohit Kharche Feb 26 '23 at 09:26

0 Answers0