Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase cloud functions not updating when deployed - still serving old version of function

I'm frustrated and confused. I'm trying to deploy my cloud functions but whenever I do they're not updating properly and despite firebase saying that they were updated successfully, keep serving the old function. Here's my index.js:

const functions = require('firebase-functions');
const admin = require('firebase-admin');

//Cloud functions for user interaction
const charge = require('./func/charge');
const addProduct = require('./func/addProduct');
const removeProduct = require('./func/removeProduct');
const updateTracking = require('./func/updateTracking');

admin.initializeApp(functions.config().firebase);

exports.charge = functions.https.onRequest(charge);
exports.updateTracking = functions.https.onRequest(addProduct);
exports.addProduct = functions.https.onRequest(removeProduct);
exports.removeProduct = functions.https.onRequest(updateTracking)

And here's my addProduct function with everything stripped away. This still doesn't update.

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const express = require('express');
const request = require('request');
const send = require('../send.js');
const cors = require('cors')({origin: '*'});

function addProduct(req, res) {
    console.log('THIS NEVER PRINTS')
    send(res, 500, {
        error: `FAILURE`
    })
}

const appAddProduct = express();
appAddProduct.use(cors);
appAddProduct.post('/', (req, res) => {

    // Catch any unexpected errors to prevent crashing
    try {
        addProduct(req, res);
    } catch(e) {
        console.log(e);
        send(res, 500, {
            error: `The server received an unexpected error. Please try again and contact the site admin if the error persists.`,
        });
    }
});

module.exports = appAddProduct

This is still serving up a success message & status code 200 from an older function with a logic error in it when this should just be serving up a 500 error every time. Something is clearly wrong with the way I've split this into multiple files as that is where the trouble started but I'm not sure where I've gone wrong. Help would be much appreciated.

like image 819
Stuart Johnson Avatar asked Mar 11 '20 15:03

Stuart Johnson


People also ask

How do I change the deployed cloud function?

While the Cloud functions are not editable on the console the work around is is tu use the Cloud Functions API to retrive the code, then you can edit the code on your local host and finally deploy it again with the corrections with the gcloud command.

Why does cloud deployment fail?

Cloud Functions deployment can fail if the entry point to your code, that is, the exported function name, is not specified correctly. Your source code must contain an entry point function that has been correctly specified in your deployment, either via Cloud console or Cloud SDK.

How many requests can handle a single cloud function?

By default each Cloud Run container instance can receive up to 80 requests at the same time; you can increase this to a maximum of 1000. Although you should use the default value, if needed you can lower the maximum concurrency. For example, if your code cannot process parallel requests, set concurrency to 1 .

How do I deploy a specific function in Firebase?

To deploy functions, run this Firebase CLI command: By default, the Firebase CLI deploys all of the functions inside index.js at the same time. If your project contains more than 5 functions, we recommend that you use the --only flag with specific function names to deploy only the functions that you've edited.

Are deploys to runtimes now disabled in the firebase CLI?

Deploys to runtimes below Node.js 10 are now disabled in the Firebase CLI. Existing Node.js 8 functions will stop executing on 2021-03-15.

What are the different versions of Firebase?

firebase-functions: 3.6.1 (Issue is present irrespective of version) firebase-tools: 8.12.1 (Issue is present irrespective of version) firebase-admin: 9.2.0 (Issue is present irrespective of version)

How do I set memory allocation and timeout in Firebase?

To set memory allocation and timeout in functions source code, use the runWith parameter introduced in Firebase SDK for Cloud Functions 2.0.0. This runtime option accepts a JSON object conforming to the RuntimeOptions interface, which defines values for timeoutSeconds and memory .


1 Answers

Your exported function names don't match the names of the imported files:

exports.charge = functions.https.onRequest(charge);
exports.updateTracking = functions.https.onRequest(addProduct);
exports.addProduct = functions.https.onRequest(removeProduct);
exports.removeProduct = functions.https.onRequest(updateTracking)

I see that three of the four functions are not paired with the same-named imports. The only one that matches is charge. I suspect you didn't intend to do this.

like image 141
Doug Stevenson Avatar answered Oct 23 '22 08:10

Doug Stevenson