0

When I tried to deploy a function on my local firebase emulator, I got this error

ERROR: Function load error: Code could not be loaded.
ERROR: Does the file exists? Is there a syntax error in your code?
ERROR: Detailed stack trace: /home/krishna/whozoo-firebase-web/functions/node_modules/firebase-functions/lib/config.js:51
        throw new Error('Firebase config variables are not available. ' +
Error: Firebase config variables are not available. Please use the latest version of the Firebase CLI to deploy this function.
    at init (/home/krishna/whozoo-firebase-web/functions/node_modules/firebase-functions/lib/config.js:51:15)
    at Object.config (/home/krishna/whozoo-firebase-web/functions/node_modules/firebase-functions/lib/config.js:29:9)
    at Object.<anonymous> (/home/krishna/whozoo-firebase-web/functions/index.js:7:31)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
ERROR: Error: Failed to deploy function.
    at exec (/usr/local/lib/node_modules/@google-cloud/functions-emulator/src/cli/controller.js:135:18)
    at ChildProcess.exithandler (child_process.js:213:5)
    at emitTwo (events.js:106:13)
    at ChildProcess.emit (events.js:191:7)
    at maybeClose (internal/child_process.js:877:16)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:226:5)

Then I retried deploying after removing this particular line of code

admin.initializeApp(functions.config())

My function is deployed successfully but when I hit the endpoint, I end up with this message

{"code": "app/no-app","message": "The default Firebase app does not exist. Make sure you call initializeApp() before using any of the Firebase services."}

How can I solve this error?

EDIT

'use strict';

const admin = require('firebase-admin');
const functions = require('firebase-functions');
const register = require('./register');

admin.initializeApp(functions.config());

exports.register = functions.https.onRequest(register.registerHandler);

register.js

'use strict';

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const cors = require('cors')

cors(req, res, () => {
let requestBody = req.body;

admin.auth().createUser({
        'email': requestBody.email,
        'emailVerified': false,
        'password': requestBody.password,
        'displayName': requestBody.firstName + ' 
        ' +requestBody.lastName
    });
});

This is the part of register.js that I believe causes the problem. The rest of the register.js contains the response message.

1 Answers1

6

Instead of this:

admin.initializeApp(functions.config());

Try this:

admin.initializeApp();

Also, put that line before you require your register module.

shraiysh
  • 106
  • 1
  • 8
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441