GV14982 / async-airtable

A lightweight npm package to handle working with the Airtable API.
https://www.npmjs.com/package/asyncairtable
MIT License
53 stars 5 forks source link

Using asyncairtable in a netlify function #82

Open cfab opened 2 years ago

cfab commented 2 years ago

An example of a netlify function using your library would be welcome. Not sure why but I always end up with an error saying that "SyntaxError: Cannot use import statement outside a module" And this happens wether I use the require or import syntax... Here is an example code:

// import { AsyncAirtable } from 'asyncairtable';
const AsyncAirtable = require('asyncairtable');

const keys = {
  atApiKey: process.env.AT_API_KEY,
  atDb: process.env.AT_DB,
}
const asyncAirtable = new AsyncAirtable(keys.atApiKey, keys.atDb);

async function getMoviesWithRelatedAsyncAirtable() {

  const movies = await asyncAirtable.select('Movies')
  const formattedMovies = formatData(movies);

  formattedMovies.forEach(async (movie) => {
    movie.DirectorFull = await getDirector(movie.Director[0]);
    if (movie && movie.Actors) {
      movie.ActorsFull = await getActors(movie.Actors);
    }
  })
  return formattedMovies;
}

async function getDirector(directorId) {
  const director = await asyncAirtable.find('Directors', directorId)
  return director.fields;
}

function getActors(actorsIds) {
  const actors = []
  actorsIds.forEach(async (actorId) => {
    const actor = await asyncAirtable.find('Actors', actorId)
    actors.push(actor.fields)
  })
  return actors;
}

function formatData(data) {
  return data.map(item => ({
    id: item.id,
    ...item.fields
  }))
}

// The actual netlify function !
exports.handler = async (event, context) => {
  try {
    const movies = await getMoviesWithRelatedAsyncAirtable();
    return {
      statusCode: 200,
      body: JSON.stringify(movies),
    };
  } catch (err) {
    console.log(err);
    return {
      statusCode: 500,
      body: JSON.stringify(err),
    };
  }
}
cfab commented 2 years ago

Oh, I haven't seen this: https://www.netlify.com/blog/2021/04/02/modern-faster-netlify-functions/ so I dit it but now I got the following error:

Request from ::1: GET /.netlify/functions/movies2
Error: Error: Error: TypeError: fetch_default is not a function
    at AsyncAirtable.<anonymous> (/Users/f/Sites/sister/node_modules/asyncairtable/src/asyncAirtable.ts:157:13)
    at Generator.throw (<anonymous>)
    at rejected (/Users/f/Sites/sister/.netlify/functions-serve/movies2/src/functions/movies2/movies2.js:3388:32)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
GV14982 commented 2 years ago

Hey there, I haven't tried running this in a Netlify function yet.

Let me see if I can find why that might be happening. I may come back here with some questions, so be on the lookout!

cfab commented 2 years ago

Thank you ! anything new ? or is it just me ?

Le 18 nov. 2021 à 03:09, Graham Vasquez @.***> a écrit :

Hey there, I haven't tried running this in a Netlify function yet.

Let me see if I can find why that might be happening. I may come back here with some questions, so be on the lookout!

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/GV14982/async-airtable/issues/82#issuecomment-972435477, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALGLKDYTTDERDP5EIRV34TDUMRN4ZANCNFSM5ICG642Q. Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.