在本地部署Firebase的云功能。 admin.initializeApp(functions.config())

时间:2017-05-25 21:48:37

标签: firebase google-cloud-platform google-cloud-functions

当我尝试在我的本地firebase模拟器上部署函数时,出现了此错误

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)

然后我在删除此特定代码行后重新部署

admin.initializeApp(functions.config())

我的功能已成功部署,但是当我点击端点时,我最终收到了此消息

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

如何解决此错误?

修改

'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
    });
});

这是register.js的一部分,我相信会导致问题。 register.js的其余部分包含响应消息。

1 个答案:

答案 0 :(得分:6)

而不是:

admin.initializeApp(functions.config());

试试这个:

admin.initializeApp();

另外,在之前放置行,需要注册模块。