我正在嘗試從前端角度將模型發送到后端,這就是我在要使用的類中定義它的方式:
documentsArray : DocumentModel
現在我有這個方法:
uploadFile(files: FileList | null): void {
if(files){
const file = files.item(0)
const reader = new FileReader()
reader.readAsDataURL(file)
reader.onload = () => {
this.documentsArray.filename = file.name,
this.api.saveDocument({body:
this.documentsArray
}).subscribe({
next: data => console.log(data),
error: error => console.log("your error: " error)
})
console.log(this.documentsArray[0])
console.log(JSON.stringify(this.documentsArray))
}
}
}
但后來我嘗試設定這部分:
this.documentsArray.filename = file.name,
我收到以下錯誤:
TypeError: Cannot set properties of undefined (setting 'filename')
該模型只有字串。有人可以告訴我我的錯誤。
uj5u.com熱心網友回復:
檔案陣列未定義。因此你無法到達 undefined.filename; 你必須像這樣宣告documentsArray = {};
uj5u.com熱心網友回復:
首先:您需要澄清documentsArray是否真的是一個陣列。
如果是,則必須在行指定索引:
this.documentsArray.filename = file.name,
如下:
this.documentsArray[0].filename = file.name,
猜你要設定第一個元素名稱
如果不是,請在在線代碼編輯器中顯示整個檔案,例如我們CodePen或StackBlitz
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/355180.html
標籤:javascript 有角的 打字稿
上一篇:在magento2中將產品添加到購物車后如何發送電子郵件?
下一篇:Git洗掉添加的大檔案提交
