Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property 'downloadURL' does not exist on type 'AngularFireUploadTask'

I have a problem with the line

this.downloadURL = task.downloadURL()

with AngularFireUploadTask even though I imported it.

    import { Component, OnInit } from '@angular/core';
    import { AuthService } from '../../core/auth.service';
    import { AngularFireStorage, AngularFireStorageReference, AngularFireUploadTask } from 'angularfire2/storage';

    import { PostService } from '../post.service';
    import { Observable } from 'rxjs/Observable';



    @Component({
      selector: 'app-post-dashboard',
      templateUrl: './post-dashboard.component.html',
      styleUrls: ['./post-dashboard.component.css']
    })
    export class PostDashboardComponent implements OnInit {

      title: string;
      image: string = null;
      content: string;

      buttonText: string = "Create Post"

      uploadPercent: Observable<number>
      downloadURL: Observable<string>

      constructor(
        private auth: AuthService,
        private postService: PostService, 
        private storage: AngularFireStorage
      ) { }

      ngOnInit() {
      }

      uploadImage(event) {
        const file = event.target.files[0]
        const path = `posts/${file.name}`
        if (file.type.split('/')[0] !== 'image') {
          return alert('only image files')
        } else {
          const task = this.storage.upload(path, file)

          this.downloadURL = task.downloadURL()

          this.uploadPercent = task.percentageChanges()
          console.log('Image Uploaded!')
          this.downloadURL.subscribe(url => this.image = url)
        }
      }

The message is:"Property 'downloadURL' does not exist on type 'AngularFireUploadTask'.".

What should I do to not have this problem.

like image 708
Redgull Avatar asked May 26 '18 10:05

Redgull


1 Answers

const task = this.storage.upload(path, file);
const ref = this.storage.ref(path);
this.uploadPercent = task.percentageChanges();
console.log('Image uploaded!');
task.snapshotChanges().pipe(
finalize(() => {
  this.downloadURL = ref.getDownloadURL()
  this.downloadURL.subscribe(url => (this.image = url));
})
)
.subscribe();

The actual change in code you will need from this video.

UPDATE - 8/30/20

For those of you that want cleaner code, use promises (await in an async function):

const task = this.storage.upload(path, file);
const ref = this.storage.ref(path);
this.uploadPercent = task.percentageChanges();

// upload image, save url
await task;
console.log('Image uploaded!');
this.image = await ref.getDownloadURL().toPromise();
like image 99
Jonathan Avatar answered Sep 28 '22 02:09

Jonathan