0

I'm trying to return some data using a firebase function for security reasons, within said data is a firebase storage file location. I want to replace the location with the download URL so the front end can display the image. Here is a mock-up of my code

// The Cloud Functions for Firebase SDK to create Cloud Functions and set
// up triggers.
const functions = require("firebase-functions");

// The Firebase Admin SDK to access Firestore.
const firebase = require("firebase-admin");

firebase.initializeApp();


exports.getDetails = functions.https.onRequest(async (request, response) => {
    
    //...loads data 

    let details = [];
    await dataArray.forEach(async (doc) => {

        let data = doc.data();

        if(doc.data().type=="Photo Select" || doc.data().type=="Document Select"){

             try{
            url =  await firebase
            .storage()
            .ref()
            .child(doc.data().value)
            .getDownloadURL();
            } catch(e){
                console.log(e)
            }

            data["value"]=url? url : "";

         

        }  
        details.push(data)
    
    })
    response.json({ 
            details: details
        })
})

but in the error logs on running I'm getting "TypeError: firebase.storage(...).ref is not a function"

Any help would be appreciated :D

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Have you included storage module? E.g `import 'firebase/storage'`. Check this similar issues: [1](https://www.codegrepper.com/code-examples/javascript/firebase.storage+is+not+a+function), [2](https://github.com/firebase/firebase-js-sdk/issues/1265), [3](https://stackoverflow.com/questions/41352150/typeerror-firebase-storage-is-not-a-function). Let me know if it helped. – mdobrucki Feb 02 '22 at 14:44
  • 1
    In Cloud Functions you are using the Admin SDK, where the Storage service is quite different from the client-side Firebase SDK for Storage. You can either generate a signed URL, or set metadata on the file to get a download URL, as shown in the top answers to this question: https://stackoverflow.com/questions/42956250/get-download-url-from-file-uploaded-with-cloud-functions-for-firebase – Frank van Puffelen Feb 02 '22 at 14:56

0 Answers0