Search code examples
node.jsfirebasegoogle-cloud-platformgoogle-cloud-functionsfirebase-storage

FirebaseStorage in CloudFunctions :TypeError: storage.ref is not a function


iam trying to access my Storage from the Firebase cloud functions

but i cant access it

its give me

TypeError: storage.ref is not a function

and here is my code

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

admin.initializeApp();
var db = admin.firestore();
var storage = admin.storage().bucket();

and my function body

exports.deletingVisaCop = functions.firestore.document('users/{user_Id}/info/visa_cop').onUpdate((change,context) =>{
    var userId = context.params.user_Id;
    // var userRef = db.collection("users").doc(userId).collection("info").doc("visa_cop");
    var data = change.after.data().state;

    var deleteData = setInterval(function () {

        var visaCopStorageRef = storage.ref();
        var ref = visaCopStorageRef.child('visa_cop');
        ref.delete().then(function(){
            console.log("Deleted")
            clearTimeout(deleteData);

            return true;
        }).catch(function(error){
            console.log("errorIs" + error)
        })

    }, 1000);

    if(data === true)
    {
        return deleteData;
    } else
    {
        return clearTimeout(deleteData);
    }
});

i want when the timer end .. the nodes in the storage delete

thank u

update

new Error

Error { ApiError: Not Found
    at Object.parseHttpRespBody (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/common/src/util.js:193:30)
    at Object.handleResp (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/common/src/util.js:131:18)
    at /user_code/node_modules/firebase-admin/node_modules/@google-cloud/common/src/util.js:496:12
    at Request.onResponse [as _callback] (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/common/node_modules/retry-request/index.js:198:7)
    at Request.self.callback (/user_code/node_modules/firebase-admin/node_modules/request/request.js:185:22)
    at emitTwo (events.js:106:13)
    at Request.emit (events.js:191:7)
    at Request.<anonymous> (/user_code/node_modules/firebase-admin/node_modules/request/request.js:1161:10)
    at emitOne (events.js:96:13)
    at Request.emit (events.js:188:7)
  code: 404,
  errors: [ { domain: 'global', reason: 'notFound', message: 'Not Found' } ],
  response: undefined,
  message: 'Not Found' }

Solution

  • With var storage = admin.storage().bucket(); you are assigning to the storage variable the default storage bucket, as explained here in the documentation.

    As detailed here, a Bucket does not have any ref() method, hence the error you are getting when doing storage.ref();.

    If you want to delete the bucket, just use its delete() method, as shown here.

    If you want to delete another bucket, i.e. the 'visa_cop' bucket, just declare it as follows and then use the delete() method:

    const visaCopBucket = admin.storage().bucket('visa_cop');
    return visaCopBucket.delete()
    .then(function(data) {
       ....
    });
    

    Note that it is not crystal clear why you introduce a "timer" in your Cloud Function. What is the functional goal?