Can not see the Firebase function deployed

2020-05-21 11:21发布

I followed the following steps:

  1. The Firebase CLI (Command Line Interface) requires Node.js and npm, which you can install by following the instructions on https://nodejs.org/

    • Installing Node.js also installs npm
  2. Once you have Node.js and npm installed, install the Firebase CLI via npm:
    npm install -g firebase-tools

    • This installs the globally available firebase command. To update to the latest version, re-run the same command
  3. Initialize your project:
    a. Run firebase login to log in via the browser and authenticate the firebase tool.

    b.Go to your Firebase project directory or create the directory

    c. Run firebase init functions

    • The tool gives you an option to install dependencies with npm. It is safe to decline if you want to manage dependencies in another way.
  4. Select associated firebase project

  5. Select Y to install dependencies with npm

  6. Move to directory setup firebase functions

  7. Edit index.js file with the function you created

  8. Run the firebase use --add to add your Firebase project

  9. Run firebase deploy --only functions to deploy the function

After all this I get the message in the terminal at deploy was completed but in the Firebase console, when i click on Functions tab there are no functions listed!?

14条回答
太酷不给撩
2楼-- · 2020-05-21 12:07

Make sure you're running at least version 3.5.0 of firebase-tools. To check which version you have, run:

firebase --version

If you're running the default setup, you can update firebase-tools using:

npm install -g firebase-tools
查看更多
闹够了就滚
3楼-- · 2020-05-21 12:11

In step 7, you have to uncomment the sample function in there and save the file. Then, in the output of the deploy command, you will be given a url for the created helloWorld function.

查看更多
Ridiculous、
4楼-- · 2020-05-21 12:11

For Cloud Functions, it is required to add your function to the special exports object (it is a Node's way of making the function accessible outside of the current file) Make sure to have index.js in your functions directory:

enter image description here

Example of a function:

// Import the Firebase SDK for Google Cloud Functions.
const functions = require('firebase-functions');
// Import and initialize the Firebase Admin SDK.
const admin = require('firebase-admin');
admin.initializeApp();



// Your function declaration. Example of a fired function when a user is created in Auth feature.
exports.myFunction = functions.auth.user().onCreate(async (user) => {
// ... your code here.
});

Then for deployment follow these steps:

  • First, if not done, make sure to have firebase-tools installed: $ npm install -g firebase-tools
  • And initialised: $ firebase init

  • For full deployment: $ firebase deploy

  • OR for functions deployment $ firebase deploy --only functions
  • OR to deploy specific functions $ firebase deploy --only functions:function1,functions:function2

A good read with a very useful example: https://codelabs.developers.google.com/codelabs/firebase-cloud-functions/#7

查看更多
ゆ 、 Hurt°
5楼-- · 2020-05-21 12:15

Quick Tip: Make sure you are exporting the function you are trying to deploy in your index.js file. Your firebase project will deploy but Cloud Functions won't be available unless they are exported.

查看更多
ら.Afraid
6楼-- · 2020-05-21 12:15

@Learn2Code I had the exact same issue. Ensure that in your index.js file, you export your function before initializing your app.

Now, go ahead and run firebase deploy from your project directory.

For example:

// Take the text parameter passed to this HTTP endpoint and insert it into the
// Realtime Database under the path /messages/:pushId/original
exports.addMessage = functions.https.onRequest(async (req, res) => {
    // Grab the text parameter.
    const original = req.query.text;
    // Push the new message into the Realtime Database using the Firebase Admin SDK.
    const snapshot = await admin.database().ref('/messages').push({original: original});
    // Redirect with 303 SEE OTHER to the URL of the pushed object in the Firebase console.
    res.redirect(303, snapshot.ref.toString());
  });

const admin = require('firebase-admin');
admin.initializeApp();
查看更多
你好瞎i
7楼-- · 2020-05-21 12:16

Had the same situation. The problem was that when I was doing

$ firebase deploy --only "myFunction" 

That filter name: myFunction, was not exactly the same as the name of the function I was trying to deploy. Silly mistake but took me a day to realize...

查看更多
登录 后发表回答