【问题标题】:Airtable not defined after import导入后未定义 Airtable
【发布时间】:2020-04-29 19:03:55
【问题描述】:

我被赋予了这项任务,以使用从带有 NodeJS 的 AirTable API 获得的数据执行一些操作。我正在使用 AirTableJS 节点库和DefinitelyTyped 空气表定义(我正在使用NestJS)。

所以,很自然,我会执行以下操作来导入必要的包。

yarn add airtable @types/airtable

然后,在与 AirTable API 交互的存储库中,我有以下内容。

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
     }
  }

但是,在运行它时,我收到以下与存储库功能相关的错误:

ReferenceError: Airtable is not defined

我在这里遗漏了什么还是我没有正确导入 Airtable 包?

谢谢。

【问题讨论】:

    标签: node.js typescript nestjs airtable


    【解决方案1】:

    因为没有导入Airtable,所以没有定义。

    这可能看起来像:

    import Airtable, { Base } from "airtable";
    

    【讨论】:

    • 我尝试了您的解决方案。而且,它似乎不喜欢我直接导入 Airtable,因为它解决了错误:Module "&lt;path to airtable module in node_modules folder&gt;" can only be default-imported using the 'esModuleInterop' flag.
    • 您应该在您的tsconfig.json 文件中设置"esModuleInterop": true,。许多 npm 模块需要它来使默认导入工作。显然,airtable 就是其中之一。 More info here
    • 或者,您可以尝试import * as Airtable from 'airtable'const Airtable = require('airtable')。但是esModuleInterop 真的不应该给你带来任何麻烦。
    • 好的。谢谢。这样可行。我想我应该开始在我的所有项目中将 esModuleInterop 设置为 true 以使事情变得更容易。 :)
    【解决方案2】:

    您没有从任何地方导入的 Airtable 类,因此 Node 和 Tyepscript 不知道它是什么。您拥有的最接近的是从airtable pacakge 导入的Base 类型。由于他们的文档不公开,因此很难说如何修复它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-04
      • 2018-01-05
      • 1970-01-01
      • 2016-06-24
      • 2018-12-11
      • 2013-08-18
      • 1970-01-01
      相关资源
      最近更新 更多