简体   繁体   中英

TYPEORM TypeError: Cannot read properties of undefined (reading 'length')

I managed to make a connection to an Azure Database but at the moment of creating the tables, the server fails with this error:

[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)

I tried to look for the solution but there isn't. I would be very thankful if you help me

Your dbTables variable is not defined. Compiler didn't catch this earlier because you used the non-null assertion operator (bang operator: ! ).

Instead of using this assertion you should adjust behaviour of your program, for example:

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

Same but shorter:

if (dbTables?.length) {}

Your case is perfect example of why overusing non-null assertion operator can be dangerous for your code base. We should use it only when for some reason we have better knowledge of types than the compiler, which is rather rare.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM