Firebase Creating and Deploying Cloud Function
Please Note: Node.js 8 runtime is deprecated. For testing purpose we use Node.js 8 runtime. It is strongly recommended to upgrade Node.js to 10 or latest
1. Download
Download Node.js from the following site: https://nodejs.org/en/
2. Install Firebase CLI
(The Firebase CLI is a utility to administer Firebase projects and perform tasks from the command prompt window)
Open command prompt and Type
npm install -g firebase-tools
3. Sign in
To sign in type following command
firebase loginThe above command will create a link (url) to login .click on the url . Most of the time it prompt to choose browser to open the url
In browser , select user account .
A warning will come 'Firebase CLI wants to access your Google Account'.
Click on 'Allow'
4. List Firebase Project
Type the following command:
firebase projects:listmake sure no space between projects: and list. The above command will show all firebase projects in your account
5. Create Directory in Windows
Create a directory 'FirebaseCloudFunction' in desktop manually. (Not using command line).
Now come back to command line and type following command
cd desktop
cd FirebaseCloudFunction6. Initialize the function
Run the command to initialize the function
firebase init functionsThe following messages will come in command prompt:-
First, let's associate this project directory with a Firebase project.
You can create multiple project aliases by running firebase use --add,
but for now we'll just set up a default project.
? Please select an option:
Please select option below:-
Use an existing project
Then ? Select a default Firebase project for this directory:
Select your project name
? What language would you like to use to write Cloud Functions?
Select
JavaScript
? Do you want to use ESLint to catch probable bugs and enforce style?
Type Yes (or Y)
Now, it will ask to install dependencies with npm. We will select yes.
It will start creating the directory structure
7. Create function
Now, Firebase initialization is completed, Go to desktop and open 'FirebaseCloudFunction' folder.
Open index.json file in notepad. and write following code.
- 'use strict';
- const functions = require('firebase-functions');
- const admin = require('firebase-admin');
- admin.initializeApp();
- // [starting allAdd]
- // [starting addFunctionTrigger]
- // adding two numbers to each other.
- exports.addDigits = functions.https.onCall((data) => {
- // [ending addFunctionTrigger]
- // [starting readAddData]
- // Numbers are passed from the client.
- const firstNumber = data.firstNumber;
- const secondNumber = data.secondNumber;
- // [ending readAddData]
- // [starting addHttpsError]
- // check that attributes are present and are numbers.
- if (!Number.isFinite(firstNumber) || !Number.isFinite(secondNumber)) {
- // Throw an HttpsError. So that the client gets the error details.
- throw new functions.https.HttpsError('invalid-argument', 'The function must be called with ' +
- 'two arguments "firstNumber" and "secondNumber" which must both be numbers.');
- }
- // [ending addHttpsError]
- // [starting returnAddData]
- // returning result.
- return {
- firstNumber: firstNumber,
- secondNumber: secondNumber,
- operator: '+',
- operationResult: firstNumber + secondNumber,
- };
- // [ending returnAddData]
- });
- // [ending allAdd]
8. Deploy function
We can deploy either using firebase deploy -only functions:function_name or all the functions using firebase deploy.
To deploy a single function, type following command.
Please don't forget to change function name. Here I'm using addDigits
firebase deploy --only functions:addDigits
9. Open Firebase console -> Cloud Functions. This will show your first cloud function
Error - Trouble shooting
1. If any error comes like this Change Node.js 8 to 10 in package files
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"lint": "eslint .",
"serve": "firebase emulators:start --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "10"
},
"dependencies": {
"firebase-admin": "^8.10.0",
"firebase-functions": "^3.6.1"
},
"devDependencies": {
"eslint": "^5.12.0",
"eslint-plugin-promise": "^4.0.1",
"firebase-functions-test": "^0.2.0"
},
"private": true
}
please check firebase.json file
{
"functions": {
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint"
],
"source": "functions"
}
}
Post a Comment