2017-06-13 77 views
0

我想在数组中的特定索引处推送布尔值。每当我跑的代码,我得到这个错误,错误类型错误:未定义在数组中的特定索引处推送布尔值

无法读取属性“推”这是我的代码:

   var check_ins = []; 
      for (var i = 0; i < this.employees.length; i++) { 
      // let check = check_ins[i]; 
      if (check_ins[i] === true) { 
       let alert = this.alertCtrl.create({ 
       title: 'Sorry,', 
       subTitle: 'ur already checked in', 
       buttons: ['OK'] 
       }); 
       alert.present(); 
       break; 
      } else if (check_ins[i] === false || check_ins[i] === undefined) { 
       let checkInTime = new TimeInModel(in_timing.date, emp_id, in_timing.time_in); 
       this.employeesService.pushTimeIn(checkInTime) 
       .subscribe(checkInTime => { 
       }, 
       error => this.errorMessage = <any>error); 
       console.log("Successfully checked-in!"); 
       check_ins[i].push(true); 
       break; 
      } 

可能是什么问题呢?是否有另一种方法来使用数组来实现相同的输出?

+1

你正在做'push'的方式是错误的。它必须是'check_ins.push(true);' –

+0

'check_ins [i] === undefined',所以你不能调用它的函数。另外,当存在一个值时,'check_ins [i]'是一个布尔值。你不能在布尔值上使用'push'功能。你需要使用'check_ins.push(true)' –

回答

0

你并不需要的价值推到truecheck_ins[i],你可以将其设置:check_ins[i] = true或推真到了数组:check_ins.push(true)

0

当您使用推,推入值被添加作为新元素到阵列。我建议你做下面的代替:

var check_ins = []; 
     for (var i = 0; i < this.employees.length; i++) { 
     // let check = check_ins[i]; 
     if (check_ins[i] === true) { 
      let alert = this.alertCtrl.create({ 
      title: 'Sorry,', 
      subTitle: 'ur already checked in', 
      buttons: ['OK'] 
      }); 
      alert.present(); 
      break; 
     } else if (check_ins[i] === false || check_ins[i] === undefined) { 
      let checkInTime = new TimeInModel(in_timing.date, emp_id, in_timing.time_in, true); 
      this.employeesService.pushTimeIn(checkInTime) 
      .subscribe(checkInTime => { 
      }, 
      error => this.errorMessage = <any>error); 
      console.log("Successfully checked-in!"); 
      check_ins[i]=true; 
      break; 
     } 
+0

这部分解决了我的问题。这就像“真实”的价值没有被保存在任何地方,因为我们可以在同一名员工身上办理两次登记手续。 想象一下,首次登机后,给定ID的状态应该设置为true,因此第二次警报消息应该弹出说已经在@Abhay Naveen – 7Guyo

+0

@ 7guyo检查,是不是你应该改变当您将check_in [i]状态设置为true时,员工的状态?只要页面不刷新,值将被保存并保持不变。 –