skip to Main Content

I am trying to use Firebase Functions to listen for file upload events in Cloud Storage, but I encountered an error during deployment that says:

functions.storage.object is not a function.

    const functions = require("firebase-functions");

    exports.processFile = functions.storage.object().onFinalize(async (object) => {try {console.log("File uploaded:", object.name);

    if (!object.name) {
      console.log("No file name found.");
      return null;
    }

    console.log(`File content type: ${object.contentType}`);
    return null;
    } catch (error) {console.error("Error processing file:", error);return null;}});

version
"firebase-admin": "^11.8.0",
"firebase-functions": "^4.3.1",
"node": "16"

I follow all instruction and also asked chatgpt. I think everything is right.but I cannot deplot it to the cloud. Hope someone can help me!

issue problem TypeError: functions.storage.object is not a function

help me to fix this issue

2

Answers


  1. You are using the 1st gen cloud functions, therefore update the required module to the following:

    const functions = require('firebase-functions/v1');
    

    Then calling onFinalize() should work. Check the documentation here:

    https://firebase.google.com/docs/functions/gcp-storage-events?gen=1st

    Login or Signup to reply.
  2. You should start with the examples in the documentation. The SDKs and APIs have changed. The version 2 API is now the default. Your import should look like this:

     const {onObjectFinalized} = require("firebase-functions/v2/storage");
    

    And your function declaration should look like this:

    exports.processFile = onObjectFinalized(async (event) => {
    // ...
    });
    

    Also note that your modules are very old versions. Use the latest firebase-functions 6.2.0 and firebase-admin 13.0.2. You should also consider upgrading the target node version since 16 is also very old and will no longer be supported in the future.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search