angular - 如何按时间对一些数据进行排序?
问题描述
我正在从 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"]);
});
}
我需要按时间对数据进行排序,例如数组中的第一项将是最新的日期数据。
我的问题是错误的,我在所有数组中都没有时间字段,这就是它显示错误的原因。代码工作正常
解决方案
推荐阅读
- reactjs - 如何从 React 的列表中删除一个项目?
- reactjs - 部署在生产环境(Digital Ocean)上的 React App 未在浏览器上加载
- python - 使用电子邮件的 Django 自定义登录不起作用
- python-3.x - 了解索引解决方案之间的差异
- css - react / css - 变换动画 - 单击时移动元素
- javascript - 在 setIcon 被称为谷歌地图后不能 setVisible
- d3.js - d3 v6 RuntimeError:拖动结束后循环定义错误
- c - 为什么在用户输入太长的输入后,这个while循环会重复?
- google-sheets - Excel公式将日期转换为季度
- regex - 你如何在 Haskell 中陈述正则表达式模式?