2

I'm relatively new to Nuxt, which is why this might be a bit of a basic question (or maybe not!).

I want to set up my Nuxt site on Netlify so that each time it builds, it fetches data from my Airtable via an API and saves a static JSON file to be used by my application.

I have the script to get the data from Airtable. The thing I'm stuck on is how do I go about setting up my Nuxt site so that on the build, the script runs and saves a local JSON file.

In case you're curious here is my script so far:

exports.handler = (event, context, callback) => {
  const Airtable = require('airtable');
  Airtable.configure({
      endpointUrl: 'https://api.airtable.com',
      apiKey: process.env.AIRTABLE_KEY
  });
  const base = Airtable.base('appovakpXJo1fg9nM');
  const eventData = []
  base('Experiments').select({
    view: 'Grid view'
  }).eachPage(
    function page (records, fetchNextPage){
      records.forEach((record) =>{
        eventData.push(record)
      })
      fetchNextPage()
  },
  function done(err){
    if (err){
      console.log(err)
    }else{
      callback(null, {
        statusCode: 200,
        body: JSON.stringify(eventData)
      })
    }
    
  })
};

Many thanks in advance for any help here.

user791793
  • 413
  • 1
  • 6
  • 19

0 Answers0