angular - 错误:无法读取未定义的属性“forEach”
问题描述
我想做的事:
我想过滤这些数组,看看是否有任何日期同时处于活动状态。
这是我的代码:
loadAllAndCheckDates (recommendedSection: RecommendedSection): boolean {
this.query()
.subscribe((res: ResponseWrapper) => { this.fromDbRecommendedSections = res.json; }, (res: ResponseWrapper) => this.onError(res.json));
return this.checkDates(recommendedSection);
}
checkDates (currentRecSec: RecommendedSection): boolean {
this.fromDbRecommendedSections.forEach((recSecDB:RecommendedSection) =>{
var dbActiveFrom = new Date(recSecDB.activeFrom);
var dbActiveTo = new Date(recSecDB.activeTo);
var currActiveFrom = new Date(currentRecSec.activeFrom);
var currActiveTo = new Date(currentRecSec.activeTo);
if(dbActiveFrom.getTime() === currActiveFrom.getTime()){
this.isDouble = true;
}if (dbActiveTo.getTime() === currActiveTo.getTime()){
this.isDouble = true;
}if(dbActiveFrom > currActiveFrom && dbActiveFrom < currActiveTo){
this.isDouble = true;
}if(dbActiveTo > currActiveFrom && dbActiveTo < currActiveTo){
this.isDouble = true;
}
}, (res: ResponseWrapper) => this.onError(res.json));
return this.isDouble;
}
问题:
可悲的是,我在控制台中收到以下错误:无法读取未定义的属性“forEach”
编辑:
下面是 fromDbRecommendedSection 的设置方式:
export class RecommendedSection implements BaseEntity {
constructor(
public id?: number,
public activeFrom?: any,
public activeTo?: any,
public identification?: string,
public recommendedSectionNames?: RecommendedSectionName,
public recommendedSectionItems?: RecommendedSectionItem[],
) {
this.recommendedSectionItems = [];
}
}
解决方案
this.fromDbRecommendedSections
您应该在您的示例中同步处理数据,在您填充之前获得响应的延迟可能太高。所以到你回来的时候this.checkDates
this.fromDbRecommendedSections
是undefined
。
尝试
loadAllAndCheckDates (recommendedSection: RecommendedSection): boolean {
this.query()
.subscribe((res: ResponseWrapper) => {
this.fromDbRecommendedSections = res.json; // needs to be an array
this.checkDates(recommendedSection)
}, (error:any) => console.log(error);
}
推荐阅读
- python - 无法使用神经网络解释预测
- powerbi - 不同粒度的两列的 DAX 条件和
- angular - Angular 组件中用于定义设置的用户环境变量
- java - 如何在不控制源代码的情况下无头运行 gui Java 应用程序?
- python - 是否可以从 jupyter 笔记本单元内的管道设置 env?
- ms-access - Microsoft Access SearchForRecord 宏操作没用?
- javascript - 修复 Invariant 违规元素类型无效:检查 `DrawerMenu` 的渲染方式
- python-3.x - ERRor 报告,Float 对象不可调用
- android-studio - 为什么我不能将一个项目中的 img 资产复制/粘贴到第二个项目的可绘制文件夹中的新目录中?
- node.js - 从 Fabric 中的本地目录加载图像