Skip to content
This repository has been archived by the owner on Sep 19, 2023. It is now read-only.

Using asyncairtable in a netlify function #82

Open
cfab opened this issue Nov 15, 2021 · 3 comments
Open

Using asyncairtable in a netlify function #82

cfab opened this issue Nov 15, 2021 · 3 comments

Comments

@cfab
Copy link

cfab commented Nov 15, 2021

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
Copy link
Author

cfab commented Nov 16, 2021

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
Copy link
Owner

GV14982 commented Nov 18, 2021

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
Copy link
Author

cfab commented Nov 18, 2021 via email

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants