我正在尝试从有角度的网站上将图像和标题上传到Firestore。
这是我的模板。
<form #form="ngForm" autocomplete="off" (submit)="onSubmit(form)">
<div class="form-group">
<input type="text" name="title" id="title" #title="ngModel" [(ngModel)]="service.timetable.title">
</div>
<div class="form-group">
<input hidden type="text" name="id" id="id" #id="ngModel" [(ngModel)]="service.timetable.id">
</div>
<div class="form-group">
<input hidden type="text" name="imgurl" id="imgurl" #imgurl="ngModel" [(ngModel)]="service.timetable.imgurl">
</div>
<div class="form-group">
<input type="file" name="timetable" id="timetable" (change)="uploadImage($event)">
</div>
<div>
</div>
<button type="submit">Submit</button>
</form>
这是我的打字稿文件
import { TimetableService } from './../../shared/timetable.service';
import { AngularFirestore } from '@angular/fire/firestore';
import { NgForm } from '@angular/forms';
import { AngularFireStorage } from '@angular/fire/storage';
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-timetable',
templateUrl: './timetable.component.html',
styleUrls: ['./timetable.component.css']
})
export class TimetableComponent implements OnInit {
selectedFile =null;
constructor(private storage: AngularFireStorage, private firebase: AngularFirestore, private service: TimetableService) { }
ngOnInit() {
this.resetForm();
}
resetForm(form?: NgForm){
if(form!=null)
form.resetForm();
this.service.timetable ={
id: null,
title:"",
imgurl:""
};
}
async onSubmit(form: NgForm){
let dataa = Object.assign({},form.value);// id, imgurl, title
delete dataa.id;
let filepath = 'timetable'+ dataa.title;
const task =this.storage.upload(filepath, this.selectedFile );
await task;
const imageurl =(await task).downloadURL;
dataa.imgurl = imageurl;
this.firebase.collection('timetabledata').add(dataa);
}
uploadImage(event){
this.selectedFile = event.target.files[0];
}
}
当我按下“提交”按钮时出现此错误
ERROR Error: Uncaught (in promise): FirebaseError: [code=invalid-argument]: Function DocumentReference.set() called with invalid data. Unsupported field value: undefined (found in field imgurl)
FirebaseError: Function DocumentReference.set() called with invalid data. Unsupported field value: undefined (found in field imgurl)
at new FirestoreError (index.cjs.js:350)
at ParseContext.push../node_modules/@firebase/firestore/dist/index.cjs.js.ParseContext.createError (index.cjs.js:20355)
at UserDataConverter.push../node_modules/@firebase/firestore/dist/index.cjs.js.UserDataConverter.parseScalarValue (index.cjs.js:20702)
at UserDataConverter.push../node_modules/@firebase/firestore/dist/index.cjs.js.UserDataConverter.parseData (index.cjs.js:20568)
at index.cjs.js:20584
at forEach (index.cjs.js:454)
at UserDataConverter.push../node_modules/@firebase/firestore/dist/index.cjs.js.UserDataConverter.parseObject (index.cjs.js:20583)
at UserDataConverter.push../node_modules/@firebase/firestore/dist/index.cjs.js.UserDataConverter.parseData (index.cjs.js:20542)
at UserDataConverter.push../node_modules/@firebase/firestore/dist/index.cjs.js.UserDataConverter.parseSetData (index.cjs.js:20407)
at DocumentReference.push../node_modules/@firebase/firestore/dist/index.cjs.js.DocumentReference.set (index.cjs.js:21384)
at resolvePromise (zone-evergreen.js:797)
at zone-evergreen.js:707
at fulfilled (tslib.es6.js:70)
at ZoneDelegate.invoke (zone-evergreen.js:359)
at Object.onInvoke (core.js:39698)
at ZoneDelegate.invoke (zone-evergreen.js:358)
at Zone.run (zone-evergreen.js:124)
at zone-evergreen.js:855
at ZoneDelegate.invokeTask (zone-evergreen.js:391)
at Object.onInvokeTask (core.js:39679)
我想将图像上传到Firestore存储中,获取downloadUrl,然后将标题和downloadUrl保存在firestore中。当我需要编辑记录时,我需要将这些数据放入表单。 我该怎么做才能解决此问题?
我对问题进行了编辑。以前,该错误已在“ id”字段中发生,并且我已将其修复。这是一个愚蠢的错误。但现在在“ imgurl”字段中发生了。
答案 0 :(得分:2)
imageUrl
是未定义的,因此在upload
方法之后,您会收到该错误,您需要执行以下操作:
onSubmit(form: NgForm){
let dataa = Object.assign({},form.value);// id, imgurl, title
delete dataa.id;
let filepath = 'timetable'+ dataa.title;
const fileRef = this.storage.ref(filepath);
const task =this.storage.upload(filepath, this.selectedFile);
task.snapshotChanges().pipe(
finalize(() => {
fileRef.getDownloadURL().subscribe(url => {
console.log(url); // <-- do what ever you want with the url..
dataa.imgurl = url;
this.firebase.collection('timetabledata').add(dataa);
});
})
).subscribe();
}
getDownloadURL()方法不再依赖于该任务,因此,为了获取url,我们应该在存储引用的顶部使用RxJS的finalize方法。
检查文档
https://github.com/angular/angularfire/blob/master/docs/storage/storage.md