0

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?

1
  • So, did you call initializeApp(), as the error message suggests? The Admin SDK can't be used without it. Commented Sep 25, 2020 at 16:38

2 Answers 2

1

Assuming you are using the Admin SDK on the cloud functions server. All you need is this:

admin.initializeApp()

Make sure you stick this outside a function, then you will have full access to the admin SDK features, which is what you're trying to use.

Sign up to request clarification or add additional context in comments.

Comments

1

The error is pointing that you need to initialize the SDK if you want to use Firebase, you can find detailed information about how to do it here, as is mentioned at the document:

"Once you have created a Firebase project, you can initialize the SDK with an authorization strategy that combines your service account file together with Google Application Default Credentials.

Firebase projects support Google service accounts, which you can use to call Firebase server APIs from your app server or trusted environment. If you're developing code locally or deploying your application on-premises, you can use credentials obtained via this service account to authorize server requests."

You should call initializeApp() before any other methods on the Admin object, for example:

admin.initializeApp({
  credential: admin.credential.cert(servicesAccount),
  databaseURL: "...",
});

const database = admin.firestore();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.