Skip to content

Commit

Permalink
object including coordinates to be served into maps >relates #18
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Dupree committed Oct 5, 2018
1 parent 3a7395d commit 8a72c3a
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions controllers/airtable.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ module.exports = (req, res, next) => {
let storage = [];
// setup postcodesIO
var PostcodesIO = require('postcodesio-client');

var postcodes = new PostcodesIO('https://api.postcodes.io', {
headers: { 'User-Agent': 'MyAwesomeApp/1.0.0' } // default {} - extra headers
});
Expand All @@ -25,7 +24,8 @@ module.exports = (req, res, next) => {
records.forEach(function (record) {
postcodeIdObj = {
id: record.id,
postcode: record.fields.Postcode
postcode: record.fields.Postcode,
coordinates: {}
}
storage.push(postcodeIdObj)
});
Expand All @@ -35,26 +35,41 @@ module.exports = (req, res, next) => {
// return postcode.postcode
// })
// }

// create array of postcodes
let postcodeArr = storage.map(postcode => {
return postcode.postcode
});
console.log(postcodeArr);

postcodeArr.forEach(postcode => {
postcodes
.lookup(postcode)
.then(function (postcode) {
console.log('longitude: ', postcode.longitude, 'latitutde: ', postcode.latitude);
coordinates = { lat: postcode.latitude, lng: postcode.longitude }
postcodeIdObj.coordinates = coordinates;
console.log(postcodeIdObj);
}, function (error) {
next(error);
});
})


// base('fonthilldummy').update('recavzIUC1T3IT1uO', {
// "Postcode": "N4 3HF"
// }, function(err, record) {
// if (err) { console.error(err); return; }
// console.log(record.get('Postcode'));
// });


// To fetch the next page of records, call `fetchNextPage`.
// If there are more records, `page` will get called again.
// If there are no more records, `done` will get called.
fetchNextPage();
},



function done(err) {
if (err) { console.error(err); next(err); }
res.send({ ...storage })
Expand Down

0 comments on commit 8a72c3a

Please sign in to comment.