This is my first time trying to deploy a function firebase function. I've written an API and I want to create a firebase function and use it.
In my project everything works on localhost and even worked when I did firebase serve --only functions, hosting
.
Since I'm only using hosting
and functions
I didn't do the initializeApp(firebaseConfig)
thing of firebase config (not sure if this is required).
My functions/index.js
is:
//functions/index.js
const functions = require("firebase-functions");
const express = require('express');
const bodyParser = require('body-parser');
var connection = require('../utils/dbconfig'); //outside functions folder
const app = express();
app.use(bodyParser.json());
// API START HERE
app.get('/getEmployees', (req, res) => {
// Here I connect to db which has it's configurations in dbConfig.js
res.send(result);
});
// API END HERE
exports.app = functions.https.onRequest(app);
I've pasted this code manually from the index.js
that I have in the main project folder (outside functions) and inside the function
folder I've another index.js
, and package.json
files which were auto-generated and I have added dependencies as I had in the package.js
on outside of the functions
folder. Then inside the functions
folder, I did npm install
.
Here is my functions/package.json
file:
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"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": "16"
},
"main": "index.js",
"dependencies": {
"firebase-admin": "^9.8.0",
"firebase-functions": "^3.14.1",
"body-parser": "~1.0.1",
"express": "~4.0.0",
"tedious": "^14.3.0"
},
"devDependencies": {
"firebase-functions-test": "^0.2.0",
"nodemon": "^2.0.15"
},
"private": true
}
Then the only firebase.json
file has these settings:
{
"hosting": {
"public": "public",
"rewrites": [
{
"source": "**",
"function": "app"
}
],
"ignore": ["firebase.json", "**/.*", "**/node_modules/**"]
}
}
When I do firebase deploy
(that deploys the functions and hosting) or firebase deploy --only functions
I get an error from which I've taken the last 10 lines:
[debug] [2022-03-08T02:48:07.963Z] <<< [apiv2][body] DELETE https://us.gcr.io/v2/ventes-4f9b6/gcf/us-central1/053feedd-aed4-4c8d-93c4-591b134374b6/cache/manifests/sha256:7b2b71f239340ebec209e230e76e303b6fd7293c8f23ee3292f23d8cf4571319 {"errors":[]}
[debug] [2022-03-08T02:48:08.022Z] Error: Failed to update function app in region us-central1
at /usr/local/lib/node_modules/firebase-tools/lib/deploy/functions/release/fabricator.js:38:11
at processTicksAndRejections (internal/process/task_queues.js:95:5)
at async Fabricator.updateV1Function (/usr/local/lib/node_modules/firebase-tools/lib/deploy/functions/release/fabricator.js:255:32)
at async Fabricator.updateEndpoint (/usr/local/lib/node_modules/firebase-tools/lib/deploy/functions/release/fabricator.js:136:13)
at async handle (/usr/local/lib/node_modules/firebase-tools/lib/deploy/functions/release/fabricator.js:75:17)
[error]
[error] Error: There was an error deploying functions
I've tried different solutions with a similar title, but nothing works so far. I have also tried installing packages again in the functions
folder, but nothing is wrong for me.
CodePudding user response:
You can't have files outside the functions folder. Only what's in the functions folder gets deployed. Move it inside of your functions folder.
var connection = require('../utils/dbconfig'); //outside functions folder
Also, functions.https.onRequest
handles parsing the body of incoming requests, so using any body parsers will likely lead to errors and you should remove it.