Looking to implement a straightforward cloud function for adding a 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});
});
During deployment, I encountered 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
Troubleshooting steps taken:
Testing the 'newUser functionality' outside of the Cloud function, where it operates correctly.
Substituting newUser function with an HTTP hello world function, which deploys and functions without issues.
Reviewing the error logs, but only finding repetitions of the message indicating erroneous code.
Expectations:
Hoping for a simpler process ;). Assuming there's a basic oversight causing the problem.
Any guidance or assistance would be greatly appreciated!