Cloud Functions Firebase v1.0 won't initialize

試著忘記壹切 提交于 2019-12-22 04:47:37

问题


With the latest update of firebase cloud functions, I am getting errors while initializing app, as well as database ref.

First Error: Following should work based on Firebase functions v1.0 documentation and samples ( https://github.com/firebase/friendlychat-web/blob/master/cloud-functions/functions/index.js )

const functions = require('firebase-functions'); 
const admin = require('firebase-admin'); 
admin.initializeApp(); //this fails

I get following error on firebase deploy for above code:

Error: Error occurred while parsing your function triggers.

Error: Failed to parse app options file: Error: ENOENT: no such file or directory, open '[object Object]'
    at FirebaseAppError.FirebaseError [as constructor] (/Users/ZZZ/dummy/functions/node_modules/firebase-admin/lib/utils/error.js:39:28)
    at FirebaseAppError.PrefixedFirebaseError [as constructor] (/Users/ZZZ/dummy/functions/node_modules/firebase-admin/lib/utils/error.js:85:28)
    at new FirebaseAppError (/Users/ZZZ/dummy/functions/node_modules/firebase-admin/lib/utils/error.js:119:28)
    at FirebaseNamespaceInternals.loadOptionsFromEnvVar (/Users/ZZZ/dummy/functions/node_modules/firebase-admin/lib/firebase-namespace.js:214:19)
    at FirebaseNamespaceInternals.initializeApp (/Users/ZZZ/dummy/functions/node_modules/firebase-admin/lib/firebase-namespace.js:64:28)
    at FirebaseNamespace.initializeApp (/Users/ZZZ/dummy/functions/node_modules/firebase-admin/lib/firebase-namespace.js:362:30)
    at Object.<anonymous> (/Users/ZZZ/dummy/functions/index.js:5:7)
    at Module._compile (module.js:643:30)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)

This error is resolved if I pass the config file (but it goes against the firebase documentation)

const functions = require('firebase-functions');
const admin = require('firebase-admin');
var config = {
    apiKey: "<APIKEY>",
    authDomain: "<DOMAIN>",
    databaseURL: "<URL>",
    projectId: "<PROJECTID>",
    storageBucket: "<BUCKET>",
    messagingSenderId: "<ID>"
  };
admin.initializeApp(config);//this works

Second Error:When I make database ref call as below , it fails and gives error:

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

var config = {
    apiKey: "<APIKEY>",
    authDomain: "<DOMAIN>",
    databaseURL: "<URL>",
    projectId: "<PROJECTID>",
    storageBucket: "<BUCKET>",
    messagingSenderId: "<ID>"
  };
  const app = admin.initializeApp(config);

  exports.updateUserProfile = functions.database.ref('/UserProfile/{userid}')
    .onCreate((snapshot, context) => {
        console.log(snapshot.val());
    });

Logs:

Running command: npm --prefix "$RESOURCE_DIR" run lint

> functions@ lint /Users/ZZZ/dummy/functions
> eslint .

✔  functions: Finished running predeploy script.
i  functions: ensuring necessary APIs are enabled...
✔  functions: all necessary APIs are enabled
i  functions: preparing functions directory for uploading...

Error: Unexpected token o in JSON at position 1

回答1:


You are experiencing this error because of a bug in firebase CLI version 3.18.1. Try uninstalling the current version and install 3.18.0 and this should solve the error.

npm uninstall -g firebase-tools
npm install -g firebase-tools@3.18.0

UPDATE: Firebase CLI version 3.18.2 has been released and the issue is now resolved.




回答2:


I was experiencing this same error. For me the problem appears to have been resolved when I followed these instructions to migrate from a Javascript project to a Typescript project.

It was a bit of a hail mary and might not actually be what fixed it. Still, if you're still stuck it won't hurt to do a quick backup and give it a shot.




回答3:


If you've updated the NPM modules, then you are now using the Stable release of Cloud functions v1. There are some minor changes that should be done to your existing code. Please follow the guide below

https://firebase.google.com/docs/functions/beta-v1-diff?utm_source=email&utm_medium=email&utm_campaign=cloud_functions_v1.0

Version 1.0.0 of the Firebase SDK for Cloud Functions introduces some important changes in the API. The primary change, a replacement of event.data format with data and context parameters, affects all asynchronous (non-HTTP) functions. The updated SDK can also be used with firebase-functions-test, a brand new unit testing companion SDK. See Unit Testing Functions for more information.



来源:https://stackoverflow.com/questions/49687932/cloud-functions-firebase-v1-0-wont-initialize

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!