I am creating a cloud function in firebase project that receives a location data from multiple android app. The function then request the distance between a device an the rest of the devices using axios library. I have installed firebase cloud function and firebase cloud admin in my windows 11 that I am using to deploy my project cloud function. This is the index.js file for my cloud function.
const {onRequest} = require("firebase-functions/v2/https"); const express = require("express"); const cors = require("cors"); const axios = require("axios"); const admin = require("firebase-admin"); const Firestore = require("@google-cloud/firestore"); const getlocation = async (res, pos) => { const endLat = pos.latitude; const endLng = pos.longitude; const googleMapsApiKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; try { const locationRef = db.collection("Drivers"); const querySnapshot = await locationRef.get(); const distanceData = []; for (const doc of querySnapshot.docs) { const data = doc.data(); const startLat = parseFloat(data.latitude); const startLng = parseFloat(data.longitude); distanceData.push({startLat, startLng}); const directionsApiUrl = `https://maps.googleapis.com/maps/api/distancematrix/json?origins=${startLat}%2C${startLng}&destinations=${endLat}%2C${endLng}&key=${googleMapsApiKey}`; try { const distanceResponse = await axios.get(directionsApiUrl); if (distanceResponse.data.status === "OK") { res.status(200).json({ response: "OK", }); } } catch (error) { res.status(500).json({error: "Failed to fetch distance data"}); } } res.status(200).json({ response: distanceData, }); doc.data()); // res.status(200).json({locations}); } catch (error) { console.error(error); res.status(500).json({error: "Failed to get locations"}); } }; app.post("/api/location", async (req, res) => { try { const pos = []; const locationData = req.body; const latitude = locationData.latitude; const longitude = locationData.longitude; const userPhone = locationData.user_phone; const userAuthToken = locationData.user_auth_token; pos.push({latitude, longitude}); if (userAuthToken === "xxxxxxxxxxxxxx") { const userRef = db.collection("users"); const userSnapshot = await userRef .where("user_phone", "==", userPhone) .get(); if (!userSnapshot.empty) { userSnapshot.docs[0].ref.update({ latitude, longitude, }); await getlocation(res, pos); } else { return res.status(400).json({ error: "User not found", }); } } else { res.status(401).json({ error: "invalid Token!", }); } } catch (error) { console.error("Error: ", error.message); res.status(500).json({ response: "failed", error: error.message, }); } }); exports.app = onRequest(app);
When I try to deploy the function using
firebase deploy --only functions
I get an error that is about the container registry.
Could not create or update Cloud Run service app, Container Healthcheck failed. Revision 'app-00033-hal' is not ready and cannot serve traffic. The user-provided container failed to start and listen on the port defined provided by the PORT=8080 environment variable. Logs for this revision might contain more information.
and also
Unhandled error cleaning up build images. This could result in a small monthly bill if not corrected. You can attempt to delete these images by redeploying or you can delete them manually
and also bring up this error in the google console cloud run logs
the user-provided container failed to start and listen on the port defined provided by the PORT=8080 environment variable. Logs for this revision might contain more information.
Which when I remove the axios library and stop requesting, the function deploys well but brings up the cleaning up build image error. I have try running my container image locally and it works fine. I have also tried deleting the image in my project google console inside the artifact registry but the error is stills showing. please help me with me with solution on how to fix this.
Solved! Go to Solution.
Hi @lupine37,
Welcome to Google Cloud Community!
Your Cloud Function container isn't listening on the required port (8080) causing deployment failure. Here's how to fix it:
These steps and Firebase documentation should help you fix the issue and deploy your Cloud Function successfully.
Documentation Reference:
Deploying Firebase Cloud Functions with HTTP triggers
Cloud Run Logs
Express.js listening on a port
Hi @lupine37,
Welcome to Google Cloud Community!
Your Cloud Function container isn't listening on the required port (8080) causing deployment failure. Here's how to fix it:
These steps and Firebase documentation should help you fix the issue and deploy your Cloud Function successfully.
Documentation Reference:
Deploying Firebase Cloud Functions with HTTP triggers
Cloud Run Logs
Express.js listening on a port
This is just flat out wrong. You cannot set a port number on a firebase function.
Thank you so much for the solution. The issue is that I installed Axios in my project directory instead of the function directory. The solution worked