【问题标题】:TYPEORM TypeError: Cannot read properties of undefined (reading 'length')TYPEORM TypeError:无法读取未定义的属性(读取“长度”)
【发布时间】:2022-01-19 17:08:37
【问题描述】:

我设法连接到 Azure 数据库,但在创建表时,服务器失败并出现以下错误:

[path]/node_modules/typeorm/driver/sqlserver/SqlServerQueryRunner.js:2309
                        if (!dbTables.length)
                                      ^

TypeError: Cannot read properties of undefined (reading 'length')
    at SqlServerQueryRunner.<anonymous> ([path]/node_modules/typeorm/driver/sqlserver/SqlServerQueryRunner.js:2309:39)
    at step ([path/node_modules/tslib/tslib.js:143:27)
    at Object.next ([path]/node_modules/tslib/tslib.js:124:57)
    at fulfilled ([path]/node_modules/tslib/tslib.js:114:62)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

我试图寻找解决方案,但没有。如果你能帮助我,我将非常感激

【问题讨论】:

    标签: javascript database typescript azure typeorm


    【解决方案1】:

    您的dbTables 变量未定义。编译器之前没有发现这一点,因为您使用了 non-null assertion operator(bang 运算符:!)。

    您应该调整程序的行为,而不是使用此断言,例如:

    // check first if variable is defined
    if (dbTables && dbTables.length) {}
    

    相同但更短:

    if (dbTables?.length) {}
    

    您的案例很好地说明了为什么过度使用非空断言运算符会对您的代码库造成危险。仅当出于某种原因我们比编译器更了解类型时才应该使用它,这种情况相当罕见。

    【讨论】:

      猜你喜欢
      • 2020-06-12
      • 1970-01-01
      • 2021-05-29
      • 2021-12-05
      • 2020-07-20
      • 2020-03-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多