【问题标题】:getting 'Cannot read property 'count' of undefined' in an if / else statement (Javascript)在 if / else 语句(Javascript)中获取“无法读取未定义的属性 'count'”
【发布时间】:2020-04-21 20:41:53
【问题描述】:

我有一个数组约会对象,每个对象都有这个键:"RecurrenceRule"。它要么是空的 (null),要么是以下内容:"RecurrenceRule": "FREQ=DAILY;INTERVAL=1;COUNT=5;"

这是一个元素的样子:

{ appointmentId: 001239,
    subject: "Something",
    Client: "Bob",
    StartTime: "2020-04-16T11:00:00.000Z",
    EndTime: "2020-04-16T11:30:00.000Z",
    km: 90,
    RecurrenceRule: null,
},

我想要做的是通过 .reduce() 函数遍历appointments,如果当前元素有"RecurrenceRule": "FREQ=DAILY;INTERVAL=1;COUNT=5;",我想取'COUNT='之后的值并将其分配给count 和如果RecurrenceRulenull,我想将1 分配给count,如果这有意义的话。方法如下:

 export class AppointmentComponent implements OnInit {
appointments;


ngOnInit(){
this.getAppointments();
}


  getAppointments(){
this.appointmentService.getAppointments().subscribe((data : any) => {
  this.appointments= data.appointment;
  this.groupByClient();

});
 };

  groupByClient(){
  var result = [];

  this.appointments.reduce(function (res, value) {
    let diff = dayjs(value.EndTime).diff(dayjs(value.StartTime), "hour",true) % 60;
    if(res[value.RecurrenceRule] !== null) {
      let count =  parseInt(value.RecurrenceRule.split("COUNT=")[1])
    } else {
      let count =  1;
    }
    if (!res[value.Client]) {

      res[value.Client] = {
        km: value.km,
        Client: value.Client,
        count: this.count,
        difference: diff * this.count     
      };
      result.push(res[value.Client]);

    } else {
      res[value.Client].km += value.km;
      res[value.Client].difference += diff;
    }

    return res;
  }, {});
}
}

但是,当我运行它时,我收到错误消息:ERROR TypeError: Cannot read property 'count' of undefined,指向this.count 行。这里出了什么问题?与嵌套的 this. 有关吗?

如果您需要更多信息,请告诉我。

【问题讨论】:

  • 您显示的所有代码在哪里?如果有更大的功能呢?您已经使用this.appointments.reduce 启动了reduce 调用,因此除非您展示如何调用该代码,否则我们无法知道this 是什么。
  • 这个函数在 ngOnInit() 函数中被调用,如果你是这个意思?
  • 我不知道这是否是我们需要的。请编辑您的问题并显示包含您显示的代码的完整方法。但是,根据你在这里所说的,this 没有引用你的对象数组,因此你的错误。如果从ngOnInit() 调用该方法,则this 可能是window。您可以通过在reduce 调用之前添加console.log(this) 来找出this 指向的内容。
  • 如果您已将数组声明为appointments,您也可以从this.appointments.reduce 中删除this 并使其成为appointments.reduct(res, value, arr),然后将this.count 替换为arr.count
  • 我已经更新了我的问题,以提供更多关于何时调用函数的上下文。 console.log(this) 之前的 this.appointments.reduce 指向整个组件

标签: javascript arrays properties this undefined


【解决方案1】:

当你用let 声明一个变量时,它的作用域是块。在您的情况下,countif(…) {…} else {…} 之外不存在

你应该写

let count;
if(value.RecurrenceRule !== null) {
      count =  parseInt(value.RecurrenceRule.split("COUNT=")[1])
    } else {
      count =  1;
    }

const count = value.RecurrenceRule ? parseInt(value.RecurrenceRule.split("COUNT=")[1]) : 1;

稍后在您的代码中使用 count 而不是 this.count

【讨论】:

  • 感谢您的解决方案。虽然它确实消除了原始错误消息,但它并没有正确应用所有计数。目的是,对于appointments 中的每个元素,如果RecurrenceRule 为空,它应该将difference 乘以1,或者乘以从split() 函数获取的值
  • 而不是res[value.Client].difference += diff; 你不应该有res[value.Client].difference += diff * count; 吗?
  • 不行还是不行。当我使用RecurrenceRule = 5 添加一个新约会时,它不会将difference 与 5 相乘,而是与 1 相乘 :(
  • res[value.RecurrenceRule] 更改为value.RecurrenceRule(我已经编辑了我的答案)
猜你喜欢
  • 1970-01-01
  • 2016-05-31
  • 2017-07-05
  • 1970-01-01
  • 1970-01-01
  • 2020-02-14
  • 1970-01-01
  • 2020-06-16
相关资源
最近更新 更多