Patrick Lumenus
Patrick Lumenus

Reputation: 1722

Airtable not defined after import

I am given this task to perform some stuff with data obtained from an AirTable API with NodeJS. I am using the AirTableJS node library with the DefinitelyTyped airtable definitions (I am using NestJS).

So, naturally, I do the following to import the necessary packages.

yarn add airtable @types/airtable

Then, in my repository that interacts with the AirTable API, I have the following.

import { User } from "../entities/user";
import { ConfigService } from "@nestjs/config";
import { Inject, Injectable } from "@nestjs/common";
import { LogService } from "src/log/services/log/log.service";
import { Base } from "airtable";


@Injectable()
export class UsersRepository {
    private readonly apiKey: string;
    private readonly baseId: string;

    constructor(@Inject(ConfigService) private readonly config: ConfigService, @Inject(LogService) private readonly log: LogService) {
        this.apiKey = this.config.get<string>('airtable.apiKey');
        this.baseId = this.config.get<string>('airtable.baseId');
    }

    async getUnmatchedUsers(): Promise<User[]> {

        const base: Base = new Airtable({apiKey: this.apiKey}).base(this.baseId);
        // other code here
     }
  }

But, when running it, I get the following error relating to the repository function:

ReferenceError: Airtable is not defined

Am I missing anything here or did I not import the Airtable package correctly?

Thanks.

Upvotes: 0

Views: 1775

Answers (3)

mvogelgesang
mvogelgesang

Reputation: 41

Currently using Airtable in a Typescript project (tsconfig below) and the following allowed me to make api calls against my Airtable base.

Node v16.5.1 Typescript v4.7.4 Airtable v0.11.4

import Airtable from 'airtable';

Airtable.configure({
  endpointUrl: "https://api.airtable.com",
  apiKey: `${process.env.AIRTABLE_API_KEY}`,
});
const base = Airtable.base(AIRTABLE_BASE);

base('myTableKey')
  .select({ 
     fields: [
       'fieldA', 'fieldB'
     ]
  })
{
  "compilerOptions": {
    // Enable top-level await, and other modern ESM features.
    "target": "ESNext",
    "module": "ESNext",
    // Enable node-style module resolution, for things like npm package imports.
    "moduleResolution": "node",
    // Enable JSON imports.
    "resolveJsonModule": true,
    // Enable stricter transpilation for better output.
    "isolatedModules": true,
    // Add type definitions for our Vite runtime.
    "types": ["node","vite/client"],
    "outDir": "dist",
    "allowSyntheticDefaultImports": true
  }
}

Upvotes: 2

Jay McDoniel
Jay McDoniel

Reputation: 70312

You don't have the Airtable class imported from anywhere, so Node and Tyepscript don't have any idea what it is. The closest you have is the Base type imported from the airtable pacakge. As their documentation isn't public, it's hard to say how to fix it.

Upvotes: 0

Alex Wayne
Alex Wayne

Reputation: 187114

It's not defined because you haven't imported Airtable.

That's probably going to look like:

import Airtable, { Base } from "airtable";

Upvotes: 1

Related Questions