lottie
Seungjun's blog
blog
TypeError: Cannot read property 'trim' of undefined

The "Cannot read property 'trim' of undefined" erroroccurs when calling the trim() method on an undefined value.


To solve the error, initialize the value to an empty string or make sure to only call the trim method on strings.


에러를 검색하니 위와 같은 결과를 찾았다. trim 메소드를 몰랐기에 mdn에서 검색해보았다.


String.prototype.trim()


trim() 메서드는 문자열 양 끝의 공백을 제거합니다. 공백이란 모든 공백문자(space, tab, NBSP 등)와 모든 개행문자(LF, CR 등)를 의미합니다.


str 문자열의 양끝의 공백을 제거한 문자열을 반환합니다.


str 문자열에 공백이 없어도 예외가 발생하지 않고 새 문자열이 반환됩니다. (본질적으로 str의 복사본).


한쪽 끝의 공백만 제거한 문자열을 반환하시려면 [trimStart()](https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/String/trimStart) 또는 [trimEnd()](https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/String/trimEnd) 메서드를 사용하세요.


즉, 위와 같은 에러 메세지는 undefined value를 지닌 대상에 공백을 제거하는 trim() 메소드를 사용해서 나온 것이다.