TS2722:无法调用可能是'undefined'的对象

问题描述 投票:0回答:1

我在Loopback框架中的对象连接器上出现错误TS2722。不明白为什么连接器对象可能无法定义

这是我的neo4j信息库中的方法:

}

async query(cypher: string, params?: any, cb?: any): Promise<any> {
// return await this.dataSource.connector.db.query(cypher, params, cb);


if (this.dataSource && this.dataSource.connector) {
  return await this.dataSource.connector.execute(cypher, params, function (err: Error, results: any) {
    // return await this.dataSource.connector.db.cypher({ "query": cypher }, function (err: Error, results: any) {
    if (err) return cb(err);
    cb(null, results);
  });
}
}

不知道为什么

希望您能提供帮助

问候

javascript neo4j loopbackjs
1个回答
0
投票

因为您的连接器可能无法连接。因此打字稿知道这一点,并且不允许您对该对象执行任何操作。

首先-您的package.json中是否有@ types / neo4j(或等效文件)?

秒-您应该告诉打字稿检查您的连接器是否有效与https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards

最终-这可能是您真正需要的https://www.typescriptlang.org/docs/handbook/advanced-types.html#instanceof-type-guards

© www.soinside.com 2019 - 2024. All rights reserved.