You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用apache-age-client时遇TypeError: ApacheAgeClient.connect is not a function错误

使用apache-age-client时出现TypeError: ApacheAgeClient.connect is not a function错误

问题描述

我尝试用apache-age-client npm包连接Apache Age实例,按照文档要求需调用类工厂的静态异步方法ApacheAgeClient.connect初始化连接,但通过tsx执行代码时返回以下错误:

TypeError: ApacheAgeClient.connect is not a function
at Database (/home/mble/Code/repos/mbledkowski/interwebs/src/db.ts:8:46)
at main (/home/mble/Code/repos/mbledkowski/interwebs/src/main.ts:90:14)

我的导入及相关代码如下:

import age from "apache-age-client";

export class Database {
  connectionPromise: Promise<age>;
  connection: age | undefined;

  constructor() {
    this.connectionPromise = age.connect({
      database: "postgresDB",
      host: "localhost",
      password: "postgresDB",
      graph: "interwebs",
      port: 5455,
      user: "interwebs",
    });
  }

  async build() {
    this.connection = await this.connectionPromise;
  }

  //[...]
}

解决方案

问题根源在于导入方式错误:apache-age-client包的ApacheAgeClient是命名导出而非默认导出,你当前的默认导入无法正确获取到包含connect方法的类。

修改导入代码为命名导入即可解决问题,同时同步修正类型声明:

// 替换原来的默认导入
import { ApacheAgeClient } from "apache-age-client";

export class Database {
  connectionPromise: Promise<ApacheAgeClient>;
  connection: ApacheAgeClient | undefined;

  constructor() {
    this.connectionPromise = ApacheAgeClient.connect({
      database: "postgresDB",
      host: "localhost",
      password: "postgresDB",
      graph: "interwebs",
      port: 5455,
      user: "interwebs",
    });
  }

  async build() {
    this.connection = await this.connectionPromise;
  }

  //[...]
}

内容的提问来源于stack exchange,提问作者Maciej Bledkowski

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 17:27:38