简体   繁体   中英

Why isn't this simple Firebase function able to deploy?

I'd like to create a simple cloud function to add een newly created user to Firestore:

./firebaseConfig.ts:

const app = initializeApp({
  ...
});

export const fireStoreDB = getFirestore(app);

./functions/index.ts:

import * as functions from 'firebase-functions';
import {DocumentData, DocumentReference, doc, setDoc} from 'firebase/firestore';
import {fireStoreDB} from '../../firebaseConfig';

const createDocumentReferenceHelper = <T = DocumentData>(
    collectionStartingPath: string,
    collectionPath: string[],
) => {
    return doc(
        fireStoreDB,
        collectionStartingPath,
        ...collectionPath,
    ) as DocumentReference<T>;
};

export const newUser = functions.auth.user().onCreate(async (user) => {
    const docRef = createDocumentReferenceHelper('users', [user.uid]);
    await setDoc(docRef, {email: user.email});
});

Upon deploying I get the following error:

✔  functions: Finished running predeploy script.
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...
✔  artifactregistry: required API artifactregistry.googleapis.com is enabled
✔  functions: required API cloudfunctions.googleapis.com is enabled
✔  functions: required API cloudbuild.googleapis.com is enabled
i  functions: preparing codebase default for deployment
i  functions: preparing functions directory for uploading...
i  functions: packaged /Users/<user>/<project>/functions (105.98 KB) for uploading
✔  functions: functions folder uploaded successfully
i  functions: updating Node.js 16 function newUser(us-central1)...

Function failed on loading user code. This is likely due to a bug in the user code. Error message: Error: please examine your function logs to see the error cause: 

https://cloud.google.com/functions/docs/monitoring/logging#viewing_logs. Additional troubleshooting documentation can be found at https://cloud.google.com/functions/docs/troubleshooting#logging. Please visit https://cloud.google.com/functions/docs/troubleshooting for in-depth troubleshooting documentation.

Functions deploy had errors with the following functions:
    newUser(us-central1)
i  functions: cleaning up build files...

Error: There was an error deploying functions

I've tried:

  • Using the same 'newUser functionality' outside of the Cloud function, which works just fine.

  • Replaced newUser function with a http hello world function, which deployed & works without problems.

  • Looked into the error logs, but they just repeat the message that the code is wrong.

I expected:

This to be way less of a hassle;). I believe I'm overthinking it and am missing something very simple.

Any help / push in the right direction will be appreciated!

Okay, turns out I way overcomplicated things. Reviewing the error logs in the Google Cloud console eventually pointed me to that I wrongly used code from another part of my project:

  • Project folder
    • functions(firebase / Google cloud)
    • src
    • firebaseConfig.ts

In the firebaseConfig.ts file I initialise Firebase and Firestore and export it to be used in the src folder:

const app = initializeApp({
  ...
});

export const fireStoreDB = getFirestore(app);

However firebase functions run in a trusted environment and therefor need to have the admin sdk enabled and initialised in the index file of the functions folder:

import * as functions from 'firebase-functions';
import * as firebaseAdmin from 'firebase-admin';

firebaseAdmin.initializeApp();
const db = firebaseAdmin.firestore();

export const newUser = functions.auth.user().onCreate((user) => {
    db.doc(`users/${user.uid}`).set({email: user.email});
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM