this

트러블슈팅

[트러블슈팅] mongoose find not working

몽구스 모듈을 이용해 스키마와 메소드를 정의하던 중 다음과 같은 에러가 발생했다. this.find is not a function 에러가 발생한 코드는 다음과 같다. userSchema.static('findById', (id, callback) => { return this.find({id:id}, callback); }); userSchema.static('findAll', (callback) => { return this.find({}, callback); }); 이는 this binding에 대한 개념이 없어서 발생한 문제였다. 기존 JavaScript에선 함수 선언 시 this에 바인딩할 객체가 동적으로 결정되었다. 하지만, arrow function을 사용하면 바인딩할 객체가 정적으로 결정된..

코택
'this' 태그의 글 목록