我在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);
});
}
}
我不明白为什么。希望您能提供帮助。
答案 0 :(得分: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