I am pretty new in Firebase and I have the following problem.
I need to create a new object into a Firestore collection named Users when a new user is registered on the Firebase Authentication service using a cloud function.
I defined and deployed this cloud function:
exports.newUserSignUp = functions.auth.user().onCreate(user => {
console.log("NEW USER CREATED: ", user);
var userObject = {
displayName : user.displayName,
email : user.email,
};
console.log("userObject: ", userObject);
let result = admin.firestore().doc('Users/').set(userObject);
return result;
});
The function is performed when a new user is created into Firebase Auth but I obtain the following error into my firebase cloud functions log:
Error: The default Firebase app does not exist. Make sure you call initializeApp() before using any of the Firebase services. at FirebaseAppError.FirebaseError [as constructor] (/srv/node_modules/firebase-admin/lib/utils/error.js:42:28) at FirebaseAppError.PrefixedFirebaseError [as constructor] (/srv/node_modules/firebase-admin/lib/utils/error.js:88:28) at new FirebaseAppError (/srv/node_modules/firebase-admin/lib/utils/error.js:123:28) at FirebaseNamespaceInternals.app (/srv/node_modules/firebase-admin/lib/firebase-namespace.js:101:19) at FirebaseNamespace.app (/srv/node_modules/firebase-admin/lib/firebase-namespace.js:452:30) at FirebaseNamespace.ensureApp (/srv/node_modules/firebase-admin/lib/firebase-namespace.js:468:24) at FirebaseNamespace.fn (/srv/node_modules/firebase-admin/lib/firebase-namespace.js:327:30) at exports.newUserSignUp.functions.auth.user.onCreate.user (/srv/lib/index.js:20:24) at cloudFunction (/srv/node_modules/firebase-functions/lib/cloud-functions.js:133:23) at /worker/worker.js:825:24
What is wrong? What am I missing? How can I fix it?
initializeApp(), as the error message suggests? The Admin SDK can't be used without it.