[英]TS2722: Cannot invoke an object which is possibly 'undefined'
I have an error TS2722 on object connector in Loopback framework.我在 Loopback 框架中的对象连接器上有一个错误 TS2722。 I don't understand why the connector object could be undefined.
我不明白为什么连接器对象可能是未定义的。
It's a method from my neo4j repository:这是我的 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);
});
}
}
I don't understand why.我不明白为什么。 Hope you can help.
希望你能帮忙。
because your connector can fail to connect.因为您的连接器可能无法连接。 so typescript knows it and don't allow you to do anything with this object.
所以打字稿知道它并且不允许你对这个对象做任何事情。
first - do you have the @types/neo4j (or equivalent) in your package.json ?首先 - 你的 package.json 中有 @types/neo4j(或等效的)吗?
second - you should tell typescript to check if your connector is valid with https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards第二 - 你应该告诉打字稿检查你的连接器是否有效https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards
ultimate - this is probably what you really need https://www.typescriptlang.org/docs/handbook/advanced-types.html#instanceof-type-guards终极 - 这可能是你真正需要的https://www.typescriptlang.org/docs/handbook/advanced-types.html#instanceof-type-guards
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.