- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在使用 Firebase 构建一个 Angular 应用程序,以将图像上传到数据库并从那里检索它。
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);
}
}
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 = "";
}
}
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;
});
}
}
现在虽然我可以上传图像并从数据库中检索它,但是我的终端显示时总是出现错误 Type 'FileUpload[][]' is not assignable to type 'AngularFireList<FileUpload[]>
Property 'query' is missing in type 'FileUpload[][]'.
知道为什么会这样吗?
如有任何帮助,我们将不胜感激。谢谢。
最佳答案
你不需要在这里使用 AngularFireList 类型,
创建一个类
export class Upload {
$key: string;
name: string;
url: string;
file: File;
progress:number;
timestamp: Date = new Date();
constructor(file: File) {
this.file = file;
}
}
在您的服务中,
uploads: Observable<Upload[]>;
getUploads() {
this.uploads = this.db.list(this.basePath).snapshotChanges().map((actions) => {
return actions.map((a) => {
const data = a.payload.val();
const $key = a.payload.key;
return { $key, ...data };
});
});
return this.uploads;
}
在你的组件中,
uploads: Observable<Upload[]>;
ngOnInit() {
this.uploads = this.uploadService.getUploads();
}
并使用 |async 改变你的 div
<div *ngIf="uploads | async; let uploads; else loading">
<div *ngFor="let upload of uploads">
<img [src]="file.url" id="image" />
</div>
</div>
关于angular - 类型 'FileUpload[][]' 不可分配给类型 'AngularFireList<FileUpload[]>',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48732151/
我有一个 Ionic 页面,它正在查询 FirebaseListObservable 以在 ion-searchbar 上进行动态搜索。它适用于 Angularfire2@4.0.0-rc.0 和 f
我在让这个搜索功能正常工作时遇到了一些麻烦。我现在已经设置好了,所以当我搜索它时它会得到正确的项目,但它的拼写必须准确,包括大写和标点符号。我希望它能够获取该项目,无论用户搜索词的大小写如何,并且如果
我想问如何将 AngularFireList 订阅到对象数组。这种方式行不通,这是我的代码的预览 Moniteurs: MoniteurModel[]; constructor(public na
我试图在 Home.html 上显示从我的 firebase 检索到的数据,但我一遍又一遍地收到相同的错误: Cannot find a differ supporting object '[obje
我很难弄清楚这个问题。运行 Ionic Serve 时出现以下错误: Property 'map' does not exist on type 'AngularFireList'. 我已经搜索了一段
当我尝试在 Angular 5/Firebase5 应用程序中订阅 AngularFireList 时,出现以下错误。 zone.js:192 Uncaught TypeError: Object(.
我对 Firebase 还很陌生,我正在学习这门类(class):https://scotch.io/tutorials/build-a-status-update-app-w-reactions-u
我正在关注 this tutorial关于如何将 angular 与 firebase 数据库连接。但在 17:30 分钟,我收到此错误: 类型“AngularFireList”上不存在属性“subs
我正在使用 Firebase 构建一个 Angular 应用程序,以将图像上传到数据库并从那里检索它。 上传文件.service.ts import {Injectable} from '@angul
我是一名优秀的程序员,十分优秀!