[英]ERROR TypeError: Cannot read property 'length' of undefined in Angular 7 Drag and Drop
[英]ERROR TypeError: Cannot read property 'length' of undefined angular 8
我做了一个网站来随机挑选一只猫,比如 Cat and Mash,但我有一个我无法理解的错误。
我有一个 JSON object ,其中有包含图像的网址。 我需要随机显示图像,但不是同一图像的 2 倍。
安慰:
为什么length
未定义?
import { Component, OnInit } from '@angular/core';
import { CatService } from '../services/cat.service';
import { CatList, Cat } from '../model/cat';
@Component({
selector: 'app-cat',
templateUrl: './cat.component.html',
styleUrls: ['./cat.component.css']
})
export class CatComponent implements OnInit {
twoCatsArray: Cat[] = [];
allcats: Cat[];
constructor(private catService: CatService) {}
ngOnInit() {
this.showMeTwoCats();
}
showMeTwoCats() {
this.catService.getCats().subscribe((cats: CatList) = > {
this.allcats = cats.images;
this.twoCatsArray = this.chooseTwoRandomCats(this.allcats);
});
}
chooseTwoRandomCats(cats: Cat[]): Cat[] {
const firstCatIndex = this.getRandomIndex(cats.length);
const secondCatIndex = this.getRandomIndex(cats.length, firstCatIndex);
return [cats[firstCatIndex], cats[secondCatIndex]];
}
getRandomIndex(maxValue: number, differentThanValue ? : number): number {
let index: number;
do {
index = this.getRandomInt(maxValue);
} while (index === differentThanValue);
return index;
}
getRandomInt(max): number {
return Math.floor(Math.random() * Math.floor(max));
}
voteForThisCat(id: string) {
const likedCatindex = this.allcats.findIndex((cat: Cat) = > cat.id === id);
const newRating = this.getIncrementedCatRatingValue(this.catService.catVote[likedCatindex].rating);
this.catService.catVote[likedCatindex].rating = newRating;
this.twoCatsArray = this.chooseTwoRandomCats(this.allcats);
}
getIncrementedCatRatingValue(rating: number | undefined): number {
return rating ? ++rating : 1;
}
}
长度和索引不是一回事。 索引从 0 开始,长度从 1 开始。因此,包含 2 项的数组的长度为 2,但第二项的索引是索引 1。您需要从长度中减去 1。 它选择了一个不存在的索引。
var cats = []; cats.push('Felix'); cats.push('Molly'); console.log('L: ' + cats.length); // L: 2 console.log('0: ' + cats[0]); // 0: Felix console.log('1: ' + cats[1]); // 1: Molly console.log('I: ' + cats[cats.length]); // I: Undefined
你对this.allCats
有什么价值观吗?
如果是
您收到错误是因为您试图从数组中不存在的 position 获取元素。
数组中的索引是一个只读属性,它是字符串中匹配项的从零开始的索引。
数组中的每个元素都有一个索引,即元素序列中的数字 position。 如果数组是 A,那么数组的第 i 个元素是 A[i]。 数组中元素的数量称为它的长度。 数组 A 的长度是 A.length。
所以如果你想从数组 A 中获取第 n 个元素,你需要访问A[n-1]
我觉得我遗漏了一些信息,但是根据我所掌握的情况,您在 chooseTwoRandomCats 中遇到了问题,您将 this.allCats 传递给该问题,这是未定义的。
你来做这件事:
allcats: Cat[];
然后你这样做:
this.catService.getCats().subscribe((cats: CatList) => {
this.allcats = cats.images;
this.twoCatsArray = this.chooseTwoRandomCats(this.allcats);
});
最终这样做:
const firstCatIndex = this.getRandomIndex(cats.length);
const secondCatIndex = this.getRandomIndex(cats.length, firstCatIndex);
那么, catService.getCats() 是否返回任何猫?
您的 CatList model 有“图像”属性吗? 它似乎是一个数组,所以它可能没有。
如果是这种情况,您将 undefined 分配给“allcats”。 如果没有,请检查猫的结果属性图像,也许您的服务没有返回任何内容。
为避免错误(但无法解决) ,您可以执行以下操作:
this.allcats = cats.images || [];
[
我复制了 json 我忘记了 json 中的关键图像
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.