I'm currently working on an Angular application integrated with Firebase for the purpose of uploading images to the database and retrieving them as well.
upload-file.service.ts
import {Injectable} from '@angular/core';
import {AngularFireDatabase, AngularFireList} from 'angularfire2/database';
import * as firebase from 'firebase';
import {FileUpload} from '../uploadingpage/fileupload';
@Injectable()
export class UploadFileService {
constructor(private db: AngularFireDatabase) {}
getFileUploads (): AngularFireList<FileUpload[]>{
return this.db.list('/uploads');
}
private basePath = '/uploads';
fileUploads: AngularFireList<FileUpload[]>;
pushFileToStorage(fileUpload: FileUpload, 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
fileUpload.url = uploadTask.snapshot.downloadURL
fileUpload.name = fileUpload.file.name
this.saveFileData(fileUpload)
}
);
}
private saveFileData(fileUpload: FileUpload) {
this.db.list(`${this.basePath}/`).push(fileUpload);
}
}
uploadingpage.component.ts
import { Component, OnInit } from '@angular/core';
import { AngularFireAuth } from 'angularfire2/auth';
import { AngularFireDatabase, AngularFireObject } from 'angularfire2/database';
import { Observable } from 'rxjs/Observable';
import * as firebase from 'firebase';
import { FirebaseAuthService } from '../auth/firebase-auth.service';
import { ItemService } from '../services/item.service';
import { UploadFileService } from '../services/upload-file.service';
import { FileUpload } from './fileupload';
@Component({
selector: 'app-uploadingpage',
templateUrl: './uploadingpage.component.html',
styleUrls: ['./uploadingpage.component.css']
})
export class ProfileComponent implements OnInit {
//for image
selectedFiles: FileList;
currentFileUpload: FileUpload;
progress: {percentage: number} = {percentage: 0};
constructor(
private afDb: AngularFireDatabase,
private _auth: FirebaseAuthService,
private itemService: ItemService,
private uploadService: UploadFileService
) { }
ngOnInit() {
}
//for image
selectFile(event) {
this.selectedFiles = event.target.files;
}
//for image
upload() {
const file = this.selectedFiles.item(0)
this.currentFileUpload = new FileUpload(file);
this.uploadService.pushFileToStorage(this.currentFileUpload, this.progress);
}
signOut(){
this._auth.signOut();
window.location.href = "";
}
}
displaypage.component.ts
import { Component, OnInit } from '@angular/core';
//additional imports
import { ItemService } from '../services/item.service';
import {FileUpload} from '../uploadingpage/fileupload';
import {UploadFileService} from '../services/upload-file.service';
import { AngularFireDatabase, AngularFireList } from 'angularfire2/database';
import * as firebase from 'firebase';
@Component({
selector: 'app-admin',
templateUrl: './displaypage.component.html',
styleUrls: ['./displaypage.component.css']
})
export class DisplaypageComponent implements OnInit {
fileUploads: AngularFireList<FileUpload[]>;
constructor(private itemService: ItemService, private uploadService: UploadFileService) { }
ngOnInit() {
this.itemService.getItems().subscribe(items => {
this.items = items;
});
this.uploadService.getFileUploads().valueChanges().subscribe(result=>{
this.fileUploads = result;
});
}
}
Although I have successfully implemented image upload and retrieval functionalities using Firebase, I encountered an error displayed in my terminal stating:
Type 'FileUpload[][]' is not assignable to type 'AngularFireList<FileUpload[]>
Property 'query' is missing in type 'FileUpload[][]'.
If anyone could provide insights into why this error is occurring, it would be highly appreciated. Thank you for your assistance.