Chats - WhatsApp Direct Chat without saving number

Firebase Creating and Deploying Cloud Function

 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 login 

The 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:list

make 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 FirebaseCloudFunction

6. Initialize the function

Run the command to initialize the function

firebase init functions

The 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.


    1. 'use strict';  
    2.   
    3. const functions = require('firebase-functions');  
    4. const admin = require('firebase-admin');  
    5. admin.initializeApp();  
    6.   
    7. // [starting allAdd]  
    8. // [starting addFunctionTrigger]  
    9. // adding two numbers to each other.  
    10. exports.addDigits = functions.https.onCall((data) => {  
    11. // [ending addFunctionTrigger]  
    12.   // [starting readAddData]  
    13.   // Numbers are passed from the client.  
    14.   const firstNumber = data.firstNumber;  
    15.   const secondNumber = data.secondNumber;  
    16.   // [ending readAddData]  
    17.   
    18.   // [starting addHttpsError]  
    19.   // check that attributes are present and are numbers.  
    20.   if (!Number.isFinite(firstNumber) || !Number.isFinite(secondNumber)) {  
    21.     // Throw an HttpsError. So that the client gets the error details.   
    22.     throw new functions.https.HttpsError('invalid-argument''The function must be called with ' +  
    23.         'two arguments "firstNumber" and "secondNumber" which must both be numbers.');  
    24.   }  
    25.   // [ending addHttpsError]  
    26.   
    27.   // [starting returnAddData]  
    28.   // returning result.  
    29.   return {  
    30.     firstNumber: firstNumber,  
    31.     secondNumber: secondNumber,  
    32.     operator: '+',  
    33.     operationResult: firstNumber + secondNumber,  
    34.   };  
    35.   // [ending returnAddData]  
    36. });  
    37. // [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

Firebase Console


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"

  }

}

It should be changed to  "npm --prefix \"%RESOURCE_DIR%\" run lint"




Post a Comment

Previous Post Next Post