【发布时间】:2021-08-16 09:30:12
【问题描述】:
我想要做的是分开两种方法。但这给我带来了一个问题。当我试图让一个函数在组件初始化上运行时。我想在上传文件时获取文件名并将数据从uploadFile 传递到getUrl event.files[0]。
我一直在尝试用顶部的其他值声明一个值,但不能,因为我得到“在初始化之前使用了属性‘文件’。”当我尝试这样做时出错。
上传服务:
import { Injectable, Inject } from '@angular/core';
import { AngularFireStorage } from '@angular/fire/storage';
import firebase from 'firebase/app';
import { User, NgAuthService } from '../auth/auth.service';
@Injectable({
providedIn: 'root',
})
export class UploadService {
constructor(private afStorage: AngularFireStorage, @Inject(NgAuthService) private user: User) { }
file: File;
url = '';
iUser = this.user.uid;
basePath = `/uploads/images/${this.iUser}`;
//method to upload file at firebase storage
async uploadFile(event: any) {
this.file = event.files[0];
const filePath = `${this.basePath}/${this.file.name}`; //path at which image will be stored in the firebase storage
await this.afStorage.upload(filePath, this.file);
if (this.file) {
this.getUrl();
} else {
console.log("Select a image please.")
}
}
//method to retrieve download url
async getUrl() {
const filePath = `${this.basePath}/${this.file.name}`; // error is coming from here.
const snap = this.afStorage.storage.ref(filePath);
await snap.getDownloadURL().then(url => {
this.url = url; //store the URL
console.log(this.url);
});
}
}
用户配置文件.component.ts:
import { Component, OnInit } from '@angular/core';
import { UploadService } from '../../storage/upload.service';
import { AngularFireStorage } from '@angular/fire/storage';
@Component({
selector: 'app-user-profile',
templateUrl: './user-profile.component.html',
styleUrls: ['./user-profile.component.scss'],
})
export class UserProfileComponent implements OnInit {
constructor(
public uploadService: UploadService,
public afStorage: AngularFireStorage,
) { }
image = this.uploadService.url;
ngOnInit() {
this.uploadService.getUrl();
}
}
堆栈跟踪错误:
ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'name' of undefined
TypeError: Cannot read property 'name' of undefined
at UploadService.<anonymous> (upload.service.ts:30)
at Generator.next (<anonymous>)
at tslib.es6.js:76
at new ZoneAwarePromise (zone.js:1387)
at __awaiter (tslib.es6.js:72)
at UploadService.getUrl (upload.service.ts:29)
at UserProfileComponent.ngOnInit (user-profile.component.ts:19)
at callHook (core.js:2526)
at callHooks (core.js:2495)
at executeInitAndCheckHooks (core.js:2446)
at resolvePromise (zone.js:1213)
at new ZoneAwarePromise (zone.js:1390)
at __awaiter (tslib.es6.js:72)
at UploadService.getUrl (upload.service.ts:29)
at UserProfileComponent.ngOnInit (user-profile.component.ts:19)
at callHook (core.js:2526)
at callHooks (core.js:2495)
at executeInitAndCheckHooks (core.js:2446)
at refreshView (core.js:9456)
at refreshEmbeddedViews (core.js:10566)
如果需要,我可以提供更多信息或任何内容。
【问题讨论】:
-
嗨@Slash1y,欢迎来到 StackOverflow。您在
UploadService中声明了变量file: File;,但没有为其赋值,因此默认为null。您将在getUrl()中获得this.file.name的未定义名称。 -
我该如何解决这个问题?因为我的项目需要它。我已经尝试了一些东西,但没有运气。 @永顺
-
@YongShun 我想使用
this.file = event.files[0],但由于我没有将任何事件传递给函数,所以它不起作用。因为那是我尝试过但不起作用的东西。当我想使用它时,我确实在函数中提供了参数event: any。你能提供一个解决方案吗?或者还有什么我可以使用并获得相同结果的东西吗? -
或者由于您的
basePath包含用户ID,您可以检索basePath 中的所有可用文件并生成下载URL。 -
但问题是,我希望每次在 ngOnInit 方法上输入我的用户配置文件组件时都调用此方法。这突然需要我提供一个参数,比如“文件名:字符串”。如果您查看组件代码,您就会明白我的意思。
标签: javascript angular typescript firebase firebase-storage