简体   繁体   English

TS2722:无法调用可能“未定义”的对象

[英]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.

相关问题 对象是在没有类的方法的情况下创建的。 打字稿错误:无法调用可能是“未定义”的对象。 ts(2722) - An object is created without a method of a class. Typescript error: Cannot invoke an object which is possibly 'undefined'. ts(2722) 为什么 TypeScript 在可选链接运算符后显示“无法调用 object 这可能是'未定义'.ts(2722)”错误? - Why is TypeScript showing “Cannot invoke an object which is possibly 'undefined'.ts(2722)” error after optional chaining operator? TypeScript:无法调用可能是“未定义”的 object - TypeScript: Cannot invoke an object which is possibly 'undefined' 无法调用子组件中可能“未定义”的对象 - Cannot invoke an object which is possibly 'undefined' in children component Ts2532、Object 可能是“未定义” - Ts2532, Object is possibly 'undefined' 无法调用可能未定义的对象 - Cannot Invoke an Object which is possible undefined 对象可能是“未定义” ts2532 - object is possibly "undefined" ts2532 打字稿-内联未定义检查不起作用(对象可能是&#39;undefined&#39;.ts(2532)) - Typescript - Inline undefined check not working (Object is possibly 'undefined'.ts(2532)) TypeScript 2:Import语句生成TS2532:“对象可能是&#39;undefined&#39;。” - TypeScript 2: Import statement generates TS2532: “Object is possibly 'undefined'.” 打字稿数组语义错误 TS2532:对象可能是“未定义” - Typescript array semantic error TS2532: Object is possibly 'undefined'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM