繁体   English   中英

上传图片,无法读取未定义的属性“订阅”

[英]Upload image, Cannot read property 'subscribe' of undefined

我有一个简单的创建帖子功能,当我填写标题,内容和图像时,我想创建一个帖子:

在此处输入图片说明

但是,当我上传图像时,进度条会填充,图像会上传到firebase,但它不允许我创建帖子,当我在控制台中对其进行测试时,会出现此错误:

在此处输入图片说明

这是代码:

 import { Component, OnInit } from '@angular/core'; import { AuthService } from '../../core/auth.service'; import { AngularFireStorage } 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.uploadPercent = task.percentageChanges() console.log('Image Uploaded!') this.downloadURL.subscribe(url => this.image = url) } } createPost() { const data = { author: this.auth.authState.displayName || this.auth.authState.email, authorId: this.auth.currentUserId, content: this.content, image: this.image, published: new Date(), title: this.title }; this.postService.create(data) this.title = '' this.content = '' this.image = '' this.buttonText = 'Post Created!' setTimeout(() => (this.buttonText = "Create Post"), 3000); } } 

就像错误说的一样, this.downloadURL是未定义的,使用前请确保已初始化

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM