使用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
相关产品推荐
相关产品推荐

