【问题标题】:why array.length + 1 returns NaN [closed]为什么 array.length + 1 返回 NaN [关闭]
【发布时间】:2020-08-03 10:21:51
【问题描述】:

我正在尝试使用下面的 for 循环添加我从 array.length 获得的长度值,但它一直返回 NaN

lent;
 ndat:any;

constructor(private afs: AngularFirestore) {
 this.afs.collection('Courses').doc(`${firebase.auth().currentUser.uid}`).collection('courses').valueChanges().subscribe(data => {

      console.log('my data',data); 
      this.ndat= data;

  this.lent=this.ndat.length;
  console.log('my lenthyyyyy',this.lent);

  });

 for (let i = 0; i < this.dat.length; i++) {
      console.log(i+this.lent);
      this.ref2=this.afs.collection('Courses').doc(`${firebase.auth().currentUser.uid}`).collection('courses').doc(`Course ${this.lent+i}`);
        this.ref2.set({
          name: this.dat[i],
          progress:'In Progress',
          duration:'time'


       });
       }
}

【问题讨论】:

  • 很难说如果你不告诉使用 this 和 array 是什么以及这些调用的上下文。在您的示例中,无需将 array.length 存储到 this

标签: typescript ionic-framework


【解决方案1】:

好像 array.length 没有定义。重现:

var array = {}; // no length property
console.log(array.length + 1); // NaN

你在哪里设置'array'和'dat'?

【讨论】:

  • 我得到了解决方案,for循环在获得长度之前就已经执行了,所以我从contructor中删除了for循环并为它创建了一个函数
猜你喜欢
  • 2019-02-20
  • 1970-01-01
  • 1970-01-01
  • 2021-10-25
  • 2013-08-19
  • 2017-05-10
  • 2016-02-28
  • 1970-01-01
  • 2018-11-07
相关资源
最近更新 更多