类型的参数' {query:{}; }'不能分配给' QueryFn'类型的参数。

时间:2018-02-08 17:33:35

标签: angular typescript firebase firebase-realtime-database angularfire2

我正在构建一个角度应用程序,我必须从firebase数据库上传和检索图像。我成功地能够上传图像,但是为了检索我在

中有以下代码

service.ts

getFileUploads(query = {}) {
this.fileUploads = this.db.list(this.basePath, {
  query: query
});
return this.fileUploads
}

我一直收到错误

  

类型的参数' {query:{}; }'不能分配给' QueryFn'。

类型的参数      

对象文字只能指定已知属性,并且'查询'类型' QueryFn'。

中不存在

以下是整个service.ts代码

import {Injectable} from '@angular/core';
import {AngularFireDatabase, AngularFireList} from 'angularfire2/database';
import * as firebase from 'firebase';

import {FileUpload} from '../profile/fileupload';

@Injectable()
export class UploadFileService {

  constructor(private db: AngularFireDatabase) {}

  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);
}

 getFileUploads(query = {}) {
this.fileUploads = this.db.list(this.basePath, {
  query: query
});
return this.fileUploads;
}
}

以下是我要显示图像的组件的component.ts

import { Component, OnInit } from '@angular/core';
import { ItemService } from '../services/item.service';
import {FileUpload} from '../profile/fileupload';
import {UploadFileService} from '../services/upload-file.service';
import { AngularFireDatabase, AngularFireList } from 'angularfire2/database'; 

@Component({
  selector: 'app-admin',
  templateUrl: './admin.component.html',
  styleUrls: ['./admin.component.css']
})
export class AdminComponent implements OnInit {

  fileUploads: AngularFireList<FileUpload[]>;

  constructor(private uploadService: UploadFileService) { }

  ngOnInit() {
    this.fileUploads = this.uploadService.getFileUploads({l});
  }
}

1 个答案:

答案 0 :(得分:2)

带有angularfire2的documentation for list querying表明list()方法的第二个参数应该是一个函数,而不是一个对象。

具体而言,来自herelist()的方法定义表明它属于

类型
list<T>(pathOrRef: PathReference, queryFn?: QueryFn): AngularFireList<T>

并且QueryFn(已定义here)属于

类型
type QueryFn = (ref: DatabaseReference) => DatabaseQuery;

所以错误是正确的,它期望你传递一个函数作为第二个参数而不是一个对象。

由于您自己的query参数没有您在此处发布的类型定义,因此我很难准确说出您认为getFileUploads方法的调用者会传入的内容,但是您可能需要在该对象类型和angularfire所期望的QueryFn类型之间进行某种转换。