Searching for the method to retrieve the image URL from the state_changed event in Firebase storage?

I've tried everything, but I just can't seem to get the downloadURL when using Vue.js for file upload. Here's the code:

Below is an image showing the console log. The image saves successfully, but the issue is that I am not getting a downloadURL.

// Function to upload file
    uploadFile(file, metadata) {
            if(file === null) return false

            let pathToUpload = this.currentChannel.id
            // Get reference to Messages.vue which returns either public or private channel
            let ref = this.$parent.getMessagesRef()
            // File path generation
            let filePath = this.getPath() + '/' + uuidV4() + '.jpg'

            // Upload file to storage
            this.uploadTask = this.storageRef.child(filePath).put(file, metadata)
            // Set upload state to "uploading"
            this.uploadState = "uploading"


            // Handle upload state change
            this.uploadTask.on('state_changed', snapshot => {
                console.log('image uploaded/state_changed in storage: ', snapshot)
                // Calculate upload progress percentage
                let percent = (snapshot.bytesTransferred / snapshot.totalBytes) * 100
                $(".progress-bar").css("width", percent+'%')
            }, error => {
                // Error handling
                this.errors.push(error.message)
                this.uploadState = 'error'
                this.uploadTask = null
            }, () => {
                // Upload finished
                this.uploadState = 'done'
                console.log('done upload')
                // Reset form
                this.$refs.file_modal.resetForm()
                // Retrieve the file URL
                let fileUrl = this.uploadTask.snapshot.downloadURL
                console.log('downloadURL(snapshot) from firebase: ', this.uploadTask.snapshot)

                // Call sendFileMessage() with fileUrl as parameter after successful upload
                this.sendFileMessage(fileUrl, ref, pathToUpload)
            })
    },

Console.log() output:

https://i.sstatic.net/dMbvR.png

Your assistance is greatly appreciated!

Answer №1

Per the Firebase documentation, it is necessary to utilize

uploadTask.snapshot.ref.getDownloadURL
within the completed callback function

uploadTask.snapshot.ref.getDownloadURL().then(function(downloadLink) {
  console.log('Download available at', downloadLink);
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Learn how to modify parent element attributes based on changes made to its child elements using JavaScript

Is there a way to select the parent element of an element that does not have any identifier? I am looking to achieve the following: <td> <a title="mySweetTitle"/> </td> I need to access the "td" element using its child "a" in ord ...

Console Error: Attempting to set the 'className' property of null object results in an

I'm experiencing difficulties setting up the code. The function should display all songs by a specific artist when their name is entered and the button is pressed. JavaScript file: /** * Utilizes AJAX to request data about artists from an online sou ...

Can you tell me the alternatives for getServerSideProps and getStaticProps in Next.js version 14?

I'm trying to wrap my head around the rendering behavior of SSR/SSG/ISR in Next.js version 14 with the updated app router. Previously, Next.js provided predefined functions like getServerSideProps for server-side fetching and processing (SSR), or getS ...

Tips on managing ASP .NET API's HttpResponseMessage for file downloads

I came across a solution on how to download a file from an asp.net API at this link: As a result, I created an API handler with the following code: public HttpResponseMessage Post([FromBody]dynamic result) { var localFilePath = graph ...

Property input not being refreshed upon using callback function

One challenge I am facing is updating a property in a child component whenever there is a push notification from Firebase. Everything seems to be set up correctly with Firebase and the property as an input in the child component. Interestingly, when I manu ...

Organize the JSON data in a particular manner

I have a set of JSON data that looks like this: [ { "name": "Event 1", "sponsors": [ { "name": "Walmart", "location": "Seattle" }, { "name": "Target", "location": "Portland" }, { ...

Issues with Firebase-admin preventing writing to the database are occurring without any error messages being displayed

Issues with Firebase admin writing to the database. The database is instantiated as follows: var db = admin.database(); A reference to the desired table is then set up: var systemsRef = db.ref("systems/"); A function is created to check if a 'sys ...

The instance does not have a defined property or method named "posts" which is being referenced during rendering

I've been attempting to reproduce the Vue tutorial example, which can be found at https://v3.vuejs.org/guide/component-basics.html#passing-data-to-child-components-with-props, but so far I haven't had any luck. Below is my code. I have a view cal ...

Encountering a white screen while loading StaticQuery on Gatsby website

I encountered an error that has been reported in this GitHub issue: https://github.com/gatsbyjs/gatsby/issues/25920. It seems like the Gatsby team is currently occupied and unable to provide a solution, so I'm reaching out here for help. Just to clar ...

Is there a way to automatically hide a div based on a checkbox value when the page loads?

How can I hide a div in my view when a checkbox is not checked upon page load? <input type="checkbox" class="k-checkbox" id="chkInvoiceStatusActive" asp-for="InvoiceStatus" value="true" /> <input t ...

What is causing the newly generated input tags to disappear from the page?

I'm having an issue where my code successfully creates new input tags based on user input, but they disappear shortly after being generated. What could be causing this to happen? <form> <input type='text' id='input' /> ...

Launch a VueJS component in a separate window

I am working on a VueJS application that currently consists of only one page and does not utilize vue-router for routing. My goal is to create a button that, when clicked, will trigger the window.open() function to display content from one of my Vue Compo ...

Leveraging data from a JSON array

After successfully retrieving a JSON array from PHP using AJAX, I am now faced with the task of utilizing specific values within the array. Although I can currently display the results as a string, my goal is to access and use individual values independen ...

Displaying a message in a modal popup once an existing modal popup has been closed - Angular

Hello, I am new to using Angular and I have a question. How can I show a success message in another popup after sending an email from a modal popup? Here is a snippet of my code: <div id="modalViewImportCode" class="modal fade btm-border" role="dialog" ...

Styling the button in jQuery to switch between disabled and enabled

I'm currently working on creating a disabled/enable button style using jQuery. You can check out my demonstration page on Codepen for reference. In the demo, you'll notice a blue submit button. When you input text into the field, the button bec ...

an unsuccessful attempt to retrieve data was made

After using the fetch function to retrieve a list of notes, nothing appears when I console.log it. I have double-checked the URL and it is correct. Here is the code snippet: import React, { useState, useEffect } from 'react' const NotesListPage ...

What is the best way to import my JavaScript function from a separate JavaScript file into my Node.js server?

Currently, I am in the process of developing a small website. As a beginner in JavaScript and Node.js, I managed to create a basic server using Node.js. However, I'm encountering challenges when it comes to loading my file.js on the server. Can someon ...

What is the best way to handle responses in axios when dealing with APIs that stream data using Server-Sent Events (S

Environment: web browser, javascript. I am looking to utilize the post method to interact with a Server-Send Events (SSE) API such as: curl https://api.openai.com/v1/completions \ -H "Content-Type: application/json" \ -H ...

Mapping Longitude and Latitude with TopoJSON and D3

Currently utilizing the UK Geo JSON found at this link to generate a UK SVG Map. The goal is to plot longitude and latitude points onto this map. The GeometryCollection place is being added to the map in the following manner: data.objects.places = { ...

Alter the properties of canvas elements

I'm a beginner with Canvas and I've created an object that resembles a ball. I've been trying to figure out how to style this ball using CSS but I'm having trouble selecting it with JavaScript or jQuery. I couldn't find any compreh ...