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

倾然丶 夕夏残阳落幕 提交于 2019-12-10 14:02:24

问题


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.


回答1:


  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.




回答2:


The method you want to call is

getDownloadURL();

Please have a look at this page.

https://github.com/angular/angularfire2/blob/master/docs/storage/storage.md

Here you can see the the method's signature is

getDownloadURL(): Observable<any>



回答3:


i got this error while using angular + firebase to upload image,The method DownloadURL() doesn't rely on the task anymore, so i will post below how i solved that error

upload(event) {
const id = Math.random().toString(36).substring(2);
this.ref = this.afStorage.ref(id);
this.task = this.ref.put(event.target.files[0]);

this.task.snapshotChanges().pipe(
  finalize(() => this.downloadURL = this.ref.getDownloadURL() ))
.subscribe();

if you want the image source url to anything,you can get it like this,

 this.task.snapshotChanges().pipe(
 finalize(() => {
  this.ref.getDownloadURL().subscribe(url => {
    console.log(url); // <-- do what ever you want with the url..
  });
}))
.subscribe();  


来源:https://stackoverflow.com/questions/50541836/property-downloadurl-does-not-exist-on-type-angularfireuploadtask

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!