gpt4 book ai didi

html - 类型错误 : Cannot read property 'File' of undefined

转载 作者:太空狗 更新时间:2023-10-29 19:33:43 26 4
gpt4 key购买 nike

我想将文件上传到我的 firebase 存储并将 url 存储在数据库中。我遵循了在此 link 上找到的示例.我有两个不同的 ts 类。当我点击提交按钮时,我的 pushFileToStorage 函数出现了一个错误,提示 TypeError: Cannot read property 'file' of undefined。谁能帮我解决这个问题?

//these are from two different class files
//fileUpload.ts
export class FileUpload {
name: string;
Url: string;
File: File;

constructor(prdFile: File) {
this.prdFile = prdFile;
}
}

//product.ts
export class Product {
$prdKey: string;
prdName: string;
prdImage: string;
prdDescription: string;
}

//product.service.ts

basePath = '/Product';
pushFileToStorage(fileUpload: FileUpload, Product: Product, progress: {
percentage: number
}) {
const storageRef = firebase.storage().ref();
const uploadTask = storageRef.child(`${this.basePath}/${fileUpload.File.name}`).put(fileUpload.File);

uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED,
(snapshot) => {
// in progress
const snap = snapshot as firebase.storage.UploadTaskSnapshot
progress.percentage = Math.round((snap.bytesTransferred / snap.totalBytes) * 100)
},
(error) => {
// fail
console.log(error)
},
() => {
// success
this.productList.push({
prdName: Product.prdName,
prdImage: Product.prdImage = fileUpload.Url = uploadTask.snapshot.downloadURL,
prdDescription: Product.prdDescription,
})
this.saveFileData(fileUpload)
}
);
}

private saveFileData(fileUpload: FileUpload) {
this.firebase.list(`${this.basePath}/`).push(fileUpload);
}

//product.component.ts
currentFileUpload: FileUpload;
onSubmit(form: NgForm) {

this.ProductService.pushFileToStorage(this.currentFileUpload, this.ProductService.selectedProduct, this.progress);
}

最佳答案

当您执行 currentFileUpload: FileUpload; 时,您没有为它提供任何值,您只是为 currentFileUpload 提供了类型。该值仍将是 undefined。将其初始化为一个实例:

currentFileUpload: FileUpload = fileUploadInstance; // Get the instance somehow

关于html - 类型错误 : Cannot read property 'File' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47919343/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com