首页 > 解决方案 > 如何按时间对一些数据进行排序?

问题描述

我正在从 Firebase 获取数据。我需要按时间排序,但显示错误

ERROR TypeError: Cannot read property 'seconds' of undefined\

这是我排序的代码

getForums(){
        this.messageService.getAllForum().pipe(map(advertisement => advertisement.sort((a :any, b: any) => b.time.seconds - a.time.seconds)
          )).subscribe((data: any)=>{
         this.advertisement = data;
         console.log(this.advertisement);


        });
}

这就是我插入数据的方式

    insert() {
     // the user details are updated in firebase user list
     this.forumDetails = {
       title: this.Name,
       body: this.description || '',
       time: new Date(),
       }
     console.log(this.forumDetails);
      this.messageService.createForum(this.forumDetails).then(resp => {
        console.log(resp);
        this.toast.show("Forum Created");
        this.router.navigate(["/home"]);
      });
  }

我需要按时间对数据进行排序,例如数组中的第一项将是最新的日期数据。

在此处输入图像描述

我的问题是错误的,我在所有数组中都没有时间字段,这就是它显示错误的原因。代码工作正常

标签: angulartypescript

解决方案


推荐阅读