我正在开发一个Angular + Firebase项目。我在firebase数据库上传数据时遇到错误。这是我在Angular2中的“firebase”服务的代码。
.FirstOrDefault()
当我点击HTML模板中的“提交”按钮时,会调用“onAddSubmit”函数,该函数会进一步调用FirebaseService的“addListing”方法。
import { AngularFirestore } from 'angularfire2/firestore';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { AngularFireDatabaseModule, AngularFireDatabase } from 'angularfire2/database';
import { AngularFireObject, AngularFireList } from 'angularfire2/database';
import * as firebase from 'firebase';
@Injectable()
export class FirebaseService {
listings ;
listing: Observable<any[]>;
folder: any;
constructor(private db: AngularFireDatabase) {
this.folder = 'listingimages';
}
getListings(){
//this.listings = this.db.list('/listings').valueChanges() as Observable<Listing[]>
this.listings = this.db.list('/listings').snapshotChanges().map(actions => {
return actions.map(action => ({ key: action.key, ...action.payload.val() }));
})
return this.listings;
}
getListingDetails(id){
this.listing = this.db.object('/listings/'+id).valueChanges() as Observable<Listing[]>;
return this.listing;
}
addListing(listing){
//Create Root Reference
let storageRef = firebase.storage().ref();
for(let selectedFile of [(<HTMLInputElement>document.getElementById('image')).files[0]]){
let path = `/${this.folder}/${selectedFile.name}`;
let iRef = storageRef.child(path);
iRef.put(selectedFile).then((snapshot) => {
listing.image = selectedFile.name;
listing.path = path;
return this.listings.push(listing);
});
}
}
}
interface Listing{
$key?: string;
title?: string;
type?: string;
image?: string;
city?: string;
owner?: string;
bedrooms?: string
}
我没有得到函数定义的错误,但是没有任何东西在firebase数据库中添加。提交时显示的错误如下:
感谢。