티스토리 뷰
객체안에 메소드 정의할 때는 mongoose모듈을 불러와서 mongoose.methods 로 했습니다.
mongoose의 Schema 내부 타입스크립트 코드를 한번 뜯어보니까 method 도 있다는걸 알게됐습니다.
mongoose.method 와 mongoose.methods 의 용도와 의미가 무엇인지 알아보기로 했습니다.
(1) mongoose.method
소스를 깠을 때는 의미는 다음과 같습니다.
Adds an instance method to documents constructed from Models compiled from this schema.
이 스키마에서 컴파일된 모델로 작성된 문서에 인스턴스 메서드를 추가 합니다.
(2) mongoose.methods
Object of currently defined methods on this schema.
이 스키마에 현재 정의된 메서드의 객체 입니다.
(추가) Mongoose 'static' methods vs 'instance' methods
static 메소드 는 클래스 인스턴스 객체를 정의하지 않아도 불러올 수 있는 메소드 입니다.
반면에 instance 메소드는 클래스 인스턴스 객체를 정의해야 불러올 수 있는 메소드 입니다.
const pokemon = new mongoose.Schema({});
// static method
pokemon.statics.getAllWithType = function(type) {
this.find({type: type});
}
// instance method
pokemon.methods.sayName = function () {
console.log(this.name);
}
const pokemonModel = mongoose.model('pokemon', pokemon);
// define instance
const squirtle = new pokemonModel({name: "꼬부기"});
// use static method
pokemonModel.getAllWithType("물");
// use instance method
squirtle.sayName();
[참고]
< mongoose: method >
https://mongoosejs.com/docs/api.html#schema_Schema-method
<mongoose: methods >
https://mongoosejs.com/docs/guide.html#methods
'Backend > 꾸준히 TIL' 카테고리의 다른 글
[mongodb] $in 와 $exist operator (0) | 2022.08.29 |
---|---|
vscode prettier 적용하기 (2) | 2022.08.29 |
[mongodb] _id 컬럼으로 검색 - find()/findOne() (0) | 2022.08.28 |
Mysql Workbench 가 예기치않게 종료되었습니다 (0) | 2022.08.24 |
리눅스명령어: lsof 와 kill (0) | 2022.08.23 |
- Total
- Today
- Yesterday
- 갓생살자
- IT용어
- vscode
- typeORM
- TypeScript
- Mongoose
- 나도 할 수 있다
- OS
- Jekyll
- MySQL
- nestjs
- 한달어스
- 클린아키텍쳐
- jest
- 참고
- MongoDB
- git
- 미완
- 습관개선
- 바이트디그리
- RDBMS
- 스마트폰중독
- 개발용어
- nestjs jest
- Nest.js
- node.js
- 한달독서
- TDD
- 디지털디톡스
- gem
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |