Capturing the file path reference from subsribe and saving it in Firebase

Currently, I have a script that is responsible for uploading my image once the form is submitted.

updateUser(user: User, privateUser: PrivateUser, dob) {
  //store img in storage
  if(this.file){
  var path = `users/${this.userID}/${this.file.name}`
  var ref = this.storage.ref(path);
  this.storage.upload(path, this.file);

    this.downloadURL = ref.getDownloadURL();
    this.refURL =this.downloadURL.subscribe(url => console.log(url) );
    console.log(this.refURL);

  }
}

I am currently working on saving the download URL as a reference point in Firebase. The script is successfully printing the desired output from

this.refURL =this.downloadURL.subscribe(url => console.log(url) );
. However, when I execute console.log(this.refURL);, it returns a subscriber instead of the actual URL value. How can I proceed with storing the URL value in Firebase?

Answer №1

When you subscribe to an observable, you essentially listen for changes and receive values in return. This is how observables work - by subscribing to them, you can perform actions based on the changes received.

this.downloadURL.subscribe(url => {
this.refURL=url;//or some other action here
})

Alternatively, you can convert it to a promise and use await to get the response.

async updateUser(user: User, privateUser: PrivateUser, dob) {
  // Store image in storage
  if(this.file){
  var path = `users/${this.userID}/${this.file.name}`
  var ref = this.storage.ref(path);
  this.storage.upload(path, this.file);

    this.downloadURL = ref.getDownloadURL();
    this.refURL= await this.downloadURL.toPromise();
    console.log(this.refURL);

  }
}

Check out the demo here!

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

Encountering "Object is possibly undefined" during NextJS Typescript build process - troubleshooting nextjs build

I recently started working with TypeScript and encountered a problem during nextjs build. While the code runs smoothly in my browser, executing nextjs build results in the error shown below. I attempted various solutions found online but none have worked s ...

The service has terminated unexpectedly because of signal: Ended prematurely: 9

I'm encountering the error 'Service exited due to signal: Killed: 9' and am unable to launch my app. I've come across information suggesting that this may be caused by memory leaks or a lengthy startup time for the app. In all honesty, ...

Unable to set a value for an Angular formcontrol

Hey there! I'm facing an issue where I am trying to assign a value to a form control in TypeScript, but it just displays as blank in HTML... :( Here is the TypeScript code snippet: constructor(private readonly messageToastService: NxMessageToastServi ...

Is there a way to retrieve the DOM element from an ngFor array?

Is there a way to access the actual element of an ngFor loop in order to manipulate it within the Component.ts file? Here's an example scenario: //.html <div *ngFor="let element of elements"> <md-card>{{element}} <but ...

What is the best way to create an "equals" method for an interface in mongodb?

export interface IHealthPlanCard { _id: string, statusId: string; } const cards: IHealthPlanCard[] = await healthPlanCardsCollection.find(...) cards.filter(card => card.statusId.equals(cardStatusId)) I encountered an issue in this scenario: An ...

Guide to configuring a not null property in Typescript Sequelize:

Hello there! I am trying to figure out how to set a not null property using TypeScript Sequelize. I have tried using the @NotNull decorator, but unfortunately it does not seem to be working. The errors I am encountering are as follows: Validation error: W ...

Steps to set angular for all items in the dropdown menu:

I am currently working on implementing a dropdown feature within my Angular application. The dropdown will display a list of shops, and when a shop is selected, it will show the content related to that particular shop. I need to add a new item called "ALL ...

Tips on updating the datepicker format to be dd/mm/yyyy in ngbdatepicker

I am currently using ng-bootstrap for a datepicker and need to change the date format from yyyy/mm/dd to dd/mm/yyyy. I have tried to make this adjustment but haven't had success. If anyone has suggestions on how to accomplish this, please help. Here ...

No GraphQL type definitions were discovered for the specified pointers: src/**/*.graphql

I am utilizing the @graphql-codegen/cli tool to automatically generate TypeScript types from my GraphQL server. Below is the content of my codegen.yml: overwrite: true schema: "http://localhost:3001/graphql" documents: "src/**/*.graphql" generates: src/ ...

The Angular 2 Router's navigation functionality seems to be malfunctioning within a service

Currently, I am facing an issue with using Angular2 Router.navigate as it is not functioning as expected. import { Injectable } from '@angular/core'; import { Http, Headers } from '@angular/http'; import { Router } from '@angular/ ...

Leveraging TypeScript's "this" keyword within an interface

I am currently working on a personalized interface where I aim to determine the type of an interface value within its implementation rather than in the interface definition, without using generics. It is important to note that these implementations will al ...

The logic behind combining elements from two arrays in JavaScript/TypeScript

Explanation of two different array formats const arr1 = [ { "elementName": "one" }, { "elementName": "two" } ] const arr2 = [ { "type": "RPT_PROPERTY_DEMOGRP", "values": [ { "label": "HH" }, { " ...

Is it possible to use ES6 import and require in Typescript 2?

In my Angular2 app, I have a class exported using ES6 modules: //File = init.todos.ts export class Init { load() { ... } } I am importing this class into another component like this: //File = todo.service.ts import { Init } from '. ...

Iterating through an Object using the ngFor directive

When going through an object, I attempted the following: pipe: @Pipe({name: 'keys'}) export class KeysPipe implements PipeTransform { transform(value, args:string[]) : any { if (!value) { return value; } let keys = []; for (le ...

Customizing table header sort icons in PrimeNG 15.4+: A step-by-step guide

The most recent update in PrimeNG brought about a change in the way sort icons are implemented. Previously, they used an i tag with CSS classes which could be customized easily using my company's icons: https://i.sstatic.net/f0Nrq.png However, the n ...

The Axios GET method retrieves a response in the form of a string that represents an object

I have developed a function that triggers an axios Request. I am working with typescript and I am avoiding the use of any for defining return data types of both the function and the axios request itself. The issue arises when the returned object contains ...

Troubleshooting problem with toggling Bootstrap accordion

Hello there! I'm currently using a bootstrap accordion within Angular, but I'm experiencing some issues with toggling. I've provided a reference to the stackblitz I created, but I can't seem to identify the cause of the toggling problem ...

Issue detected in the ngx-joyride package: Error found in ./node_modules/ngx-joyride/assets/images/close.svg

During my build process, I encountered an error with ngx-joyride: ERROR in ./node_modules/ngx-joyride/assets/images/close.svg Module parse failed: Unexpected token (1:0) You may need an appropriate loader to handle this file type." <line x1="1" y1=" ...

Issue encountered in Typescript: callback functions are returning undefined value when called from a superclass

As a newcomer to TypeScript and Node.js, I decided to dive into something new by exploring Angular, Node, and Express. While attempting to practice good project structure practices in Express by breaking it down into smaller parts, I encountered an issue. ...

Issue with Angular: Child component not receiving data after successful parent component call

I'm currently working with a parent and child component setup. Within the child component, I have a button configured like this: //child.component.html <button mat-raised-button [disabled]="!form.valid || submitButtonDisable" type = 'Submi ...