Wpis z mikrobloga

#nodejs #expressjs #keystonejs Robię sobie geocoder. Ma przed zapisem postu z adresem pobierać latlony. No i ślicznie, tylko to działa asynchronicznie, więc w efekcie post zapisuje się szybciej niż zdążą wrócić dane z google api .. ktoś coś?

kodzik: (tak, wiem, że adres z-----y, potem wypluję go prawilnie ;) )

Place.schema.pre(


'save'





function
```**```
(next) {

    
```**```
if
```**```
 (!
```**```
this
```**```
.location.lat || 
```**```
this
```**```
.isModified(
```_```
'location'
```_```
)) {

            
```**```
var
```**```
 address = 

                    
```**```
this
```**```
.location.street 

                    + 
```_```
' '
```_```
 

                    + 
```**```
this
```**```
.location.number 

                    + 
```_```
', '
```_```
 

                    + 
```**```
this
```**```
.location.city,

``````

            geocoderProvider = 
```_```
'google'
```_```
,

                httpAdapter = 
```_```
'https'
```_```
,

``````

                extra = {

                    apiKey: 
```_```
'blabla'
```_```
,

                    formatter: 
```**```
null
```**```

                },

                lat, lng;

``````

                
```**```
var
```**```
 geocoder = require(
```_```
'node-geocoder'
```_```
).getGeocoder(geocoderProvider, httpAdapter, extra);                

                geocoder.geocode(address)

                    .then(
```**```
function
```**```
(res) {

                        lat = res[0].latitude;

                        lng = res[0].longitude; 

``````

                        
```**```
this
```**```
.location.lat = lat;        

                                
```**```
this
```**```
.location.lng = lng;

``````

                                
```**```
this
```**```
.save(
```**```
function
```**```
(err){

                                        console.log(lat)

                                        console.log(err)

                                })                

                    })

                    .
```**```
catch
```**```
(
```**```
function
```**```
(err) {

                        console.log(err);

                    });

    }

    next();

});

#webdev
  • Odpowiedz
  • Otrzymuj powiadomienia
    o nowych komentarzach