programing

null의 '포함' 속성을 읽을 수 없습니다.

prostudy 2022. 6. 29. 20:32
반응형

null의 '포함' 속성을 읽을 수 없습니다.

JavaScript에서 Vue.js를 사용하고 있습니다.

다음 오브젝트 배열이러한오브젝트 배열이 있습니다.products그리고 모든 물체는property불렀다smallest_unit_barcode다음과 같은 바코드로 제품만 필터링하고 싶다.value그래서 다음 기능을 수행했습니다.

if (value != '') {
    var results = this.products.filter(obj=>obj.smallest_unit_barcode.includes(value));
    var results = results.slice(Math.max(results.length - 20, 0))
    this.pos_quick_lunch = results;
}

모든 것이 잘 작동하지만obj.smallest_unit_barcode == null, 다음의 에러가 표시됩니다.

Error in v-on handler: "TypeError: Cannot read property 'includes' of null"

내가 어떻게 무시하지?null제품 어레이를 필터링할 때 어떤 값을 얻을 수 있습니까?

비교null속성에 액세스하기 전에 다음을 수행합니다.

obj => obj.smallest_unit_barcode !== null && obj.smallest_unit_barcode.includes(value)

왜냐면&&단락 중. 왼쪽 피연산자가 false로 평가되면 오른쪽 피연산자가 평가되지 않습니다.

언급URL : https://stackoverflow.com/questions/62714171/cannot-read-property-includes-of-null

반응형