Skip to content

Commit

Permalink
creating airtable object to be sent to react >relates #18
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Dupree committed Oct 5, 2018
1 parent b924cd3 commit 8c2cdd9
Showing 1 changed file with 40 additions and 42 deletions.
82 changes: 40 additions & 42 deletions controllers/airtable.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,34 @@ module.exports = (req, res, next) => {
});
var base = Airtable.base('apphdQNWTLdRQbOOg');

// set up storage array to be sent to frontend
let storage = [];
// set up storageUpdaterArr array to be sent to
let storageUpdaterArr = [];

// 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
});

// start query
// start query to update airtable view no_geolocation
base('fonthilldummy').select({
// Selecting the first 3 records in Grid view:
maxRecords: 3,
pageSize: 3,
maxRecords: 100,
pageSize: 100,
view: "no_geolocation"
}).eachPage(function page(records, fetchNextPage) {
// This function (`page`) will get called for each page of records.
const airtableRecords = records

airtableRecords.forEach(function (record) {
// set up objet to be populated using postcodesIO and sent back to airtable
postcodeIdObj = {
id: record.id,
postcode: record.fields.Postcode,
coordinates: {}
}
storage.push(postcodeIdObj)
storageUpdaterArr.push(postcodeIdObj)
});
// create postcode object to be sent to geolocation api
let postcodeObj = {
"postcodes": storage.map(postcode => {
"postcodes": storageUpdaterArr.map(postcode => {
return postcode.postcode
})
}

request.post('https://api.postcodes.io/postcodes', {
json: postcodeObj,

}, (error, res, body) => {
if (error) {
// connsole.log(error)
Expand All @@ -58,44 +48,52 @@ module.exports = (req, res, next) => {
}
console.log(`statusCode: ${res.statusCode}`)


// populate storageUpdate array coordinates
for (let i = 0; i < body.result.length; i++) {
storage[i].coordinates = {
storageUpdaterArr[i].coordinates = {
lat: body.result[i].result.latitude,
lng: body.result[i].result.longitude
}
}

// console.log(storage[0].coordinates)
for (let i = 0; i < storage.length; i++){
// console.log(storage.length)

base('fonthilldummy').update(storage[i].id, {
'geolocation': JSON.stringify(storage[i].coordinates)
}, function(err,record) {
if (err){console.log(err); return;}
// send coords and id to update no_geolocation view
for (let i = 0; i < storageUpdaterArr.length; i++) {
base('fonthilldummy').update(storageUpdaterArr[i].id, {
'geolocation': JSON.stringify(storageUpdaterArr[i].coordinates)
}, function (err, record) {
if (err) { console.log(err); return; }
console.log(record.get('geolocation'));
});


});
}



})

// 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();
},
// set up api call to get airtable data
base('fonthilldummy').select({
maxRecords: 1,
pageSize: 3,
view: "Grid view"
}).eachPage(function page(records, fetchNextPage) {
records.forEach(function (record) {
// set up objet to be populated using postcodesIO and sent back to airtable
airtableObj = {
id: record.id,
postcode: record.fields.Postcode,
coordinates: record.fields.geolocation
}
// console.log(airtableObj);
})
fetchNextPage();
},
function done(err) {
if (err) { console.error(err); next(err); }
res.send(airtableObj)
}
)



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

});
);

}

0 comments on commit 8c2cdd9

Please sign in to comment.