카테고리 없음

[node.js] 몽구스 스키마 사전 저장 기능에서 비동기 호출 사용

행복을전해요 2021. 2. 19. 02:49

geocoder.geocode콜백 에서 해당 필드를 설정 하므로 this더 이상 위치 인스턴스를 참조하지 않습니다.

대신 다음과 같이하십시오.

LocationSchema.pre('save', function(next) {
  var doc = this;
    geocoder.geocode(this.address, function ( err, data ) {
        if(data && data.status == 'OK'){
              doc.lat = data.results[0].geometry.location.lat;
                    doc.lng = data.results[0].geometry.location.lng;
                        }
                            next();
                              });
                              });
                              


출처
https://stackoverflow.com/questions/22079879