typescript - Angular firebase ค่าฟิลด์ที่ไม่รองรับ
ฉันมีไฟล์ uplaod.ts :
export class Project {
$key: string;
file: File;
name: string;
title: string; //can't write this to db with ngModel (look below)
cat: string; //can't write this to db with ngModel (look below)
url: string;
progress: number;
createdAt: Date = new Date();
constructor(file: File) {
this.file = file;
}
}
แล้วใน upload.service ฉันมีฟังก์ชัน pushUpload() ของฉันพร้อมกับ savefiledata() :
//files
uploads:Observable<Project[]>;
project:AngularFirestoreCollection<Project[]>
//pushUpload uploads to storage
pushUpload(upload: Project) {
const storageRef = firebase.storage().ref();
const uploadTask = storageRef.child(`${this.basePath}/${upload.file.name}`).put(upload.file);
uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED,
(snapshot: firebase.storage.UploadTaskSnapshot) => {
// upload in progress
const snap = snapshot;
upload.progress = (snap.bytesTransferred / snap.totalBytes) * 100
},
(error) => {
// upload failed
console.log(error);
},
() => {
// upload success
if (uploadTask.snapshot.downloadURL) {
upload.url = uploadTask.snapshot.downloadURL;
upload.name = upload.file.name;
this.fire.collection(`users/${this.auth.userId}/projects`).add( { photoURL: upload.url, file: upload.file.name})
this.saveFileData(upload);
return;
} else {
console.error('No download URL!');
}
},
);
}
// Writes the file details to the realtime db
private saveFileData(upload: Project) {
this.db.list(`profile/${this.auth.userId}/project`).push(upload);
}
ฉันสามารถอัปโหลดไฟล์ไปยังที่จัดเก็บและบันทึก URL/ชื่อไฟล์ในฐานข้อมูลได้ แต่ฉันไม่สามารถบันทึก Title และ Cat ด้วย ngModels ได้
upload.ts :
selectedFiles: FileList | null;
currentUpload: Project;
proinfo = {} as Project;
uploadSingle() {
const file = this.selectedFiles;
if (file && file.length === 1) {
this.currentUpload = new Project(file.item(0));
this.navSrv.pushUpload(this.currentUpload);
} else {
console.error('No file found!');
}
}
upload.html :
<div class="box">
<h3>Single File Upload</h3>
<label>
<input type="file" class="button" (change)="detectFiles($event)">
</label>
<label>
<input [(ngModel)]="proinfo.title" type="text" class="button">
</label>
<label>
<input [(ngModel)]="proinfo.cat" type="text" class="button">
</label>
<button class="button is-primary"
[disabled]="!selectedFiles"
(click)="uploadSingle()">
Upload Single
</button>
<hr>
<h3>Multiple File Upload</h3>
<label>
<input type="file" class="button" (change)="detectFiles($event)" multiple>
</label>
<button class="button is-primary"
[disabled]="!selectedFiles"
(click)="uploadMulti()">
Upload Multiple
</button>
</div>
ฉันไม่ได้ข้อผิดพลาดเลย ข้อมูลจะไม่ถูกบันทึกในฐานข้อมูล (บันทึกเฉพาะชื่อ URL และความคืบหน้า)
คำถามหลักคือ: จะบันทึกชื่อเรื่องและหมวดหมู่ในฐานข้อมูลพร้อมกับ URL, ชื่อ, ความคืบหน้าในรายการเดียวกันได้อย่างไร?