【问题标题】:Angular 5 firebase database get one itemAngular 5 firebase 数据库获得一项
【发布时间】:2018-10-14 15:28:43
【问题描述】:

我有很多项目:

export class Project {
  $key: string;
  file: File;
  name: string;
  title: string;
  cat: string;
  url: string;
  progress: number;
  createdAt: Date = new Date();

  constructor(file: File) {
    this.file = file;
  }
}

我把它们都上传到:

  uploads: Observable<Project[]>;

  private saveFileData(upload: Project) {
    this.db.list(`profile/${this.auth.userId}/project`).push(upload);
}

然后我想得到一个:

 uploads: Observable<Project[]>;
getOne(){
  this.uploads = this.db.list(`profile/${this.auth.userId}/project/${this.projectId}`);
}

在这种情况下,我在 this.uploads 上收到错误

(Angularfirelist 不能分配给 Observable。)

然后我尝试了这个:

  uploads: AngularFireList<Project[]>;

getOne(){
  this.uploads = this.db.list(`profile/${this.auth.userId}/project/${this.projectId}`);
}

错误:

错误错误: InvalidPipeArgument: '[object Object]' 用于管道 '异步管道'

如何获得那个项目?

【问题讨论】:

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


    【解决方案1】:

    添加valueChanges()只获取数据,或添加snapshotChanges()获取数据的有效负载:

    this.uploads = this.db.list(`profile/${this.auth.userId}/project/${this.projectId}`).valueChanges();
    

    【讨论】:

    • 它将所有字符串作为新对象。
    • 但是您在模板中使用async 管道,例如 - &lt;div *ngFor="let item of uploads | async"&gt;?
    • 如果我只想显示一个对象,我假设我需要使用对象而不是列表
    • 所以你可以这样做{{ uploads | async }}
    • 是的,它有效,如果你能提供帮助,下一个问题:如何像这样上传我的项目:project1/projec1 值,project2/project2 值,
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    相关资源
    最近更新 更多