使用 --detectOpenHandles 参数运行测试后出现以下错误
\n Jest has detected the following 1 open handle potentially keeping Jest from exiting:\n\n \xe2\x97\x8f PROMISE\n\n 18 |\n 19 | mongoose.Promise = global.Promise;\n> 20 | mongoose.connect(config.database.link, config.database.options);\n | ^\n 21 |\n 22 |\n 23 | app.use(cors());\nRun Code Online (Sandbox Code Playgroud)\n但我的测试包括 mongoose.disconnect()
\nafterAll(() => {\n return new Promise(res => mongoose.disconnect(() => {\n res();\n }));\n});\nRun Code Online (Sandbox Code Playgroud)\n我尝试将 afterAll 函数更改为如下所示:
\nafterAll(async () => {\n await mongoose.disconnect();\n await mongoose.connection.close();\n});\nRun Code Online (Sandbox Code Playgroud)\n我还尝试在 afterAll() 内部调用 con.disconnect
\napp.con = mongoose.connect(config.database.link, config.database.options);\n\n// inside of …Run Code Online (Sandbox Code Playgroud) 如何检查嵌套数组是否为空?当没有数据时,数组看起来像这样:
const data = [ [ ] ]
Run Code Online (Sandbox Code Playgroud)
如果它有数据,它看起来像这样:
const data = [
[{"Product": 7 }]
]
Run Code Online (Sandbox Code Playgroud)
要检查它是否为空,我正在执行以下操作,但不起作用:
if (!Array.isArray(data[0][0]) || data[0][0].length === 0) {
return "Data is empty"
}
Run Code Online (Sandbox Code Playgroud)
我不明白的是为什么!Array.isArray(data[0][0])返回true,这意味着嵌套数组不是一个数组(这很奇怪,因为它是一个数组,只是空的)。根据文档,Array.isArray([])返回 true 那么如何!Array.isArray(data[0][0]返回 true?
该data[0][0].length部分抛出一个错误,指出"TypeError: "data[0][0] is undefined""。为什么会这样呢?
这就引出了一个问题:如何检查嵌套数组是否为空?