Javascript Dexie STARTSWITSIGNORECASE()函数数组未定义

Javascript Dexie STARTSWITSIGNORECASE()函数数组未定义,javascript,node.js,electron,dexie,dexiejs,Javascript,Node.js,Electron,Dexie,Dexiejs,我正在使用StartWithIgnoreCase查询dexie数据库,并在数组中推送结果,但在打印或使用它时,它会抛出一个未定义的错误 我曾尝试使用JSON.stringify、toString、String将其转换为字符串并在控制台上打印,但仍然显示未定义 将整个阵列打印到控制台时,显示普通阵列() 当我使用console时,我至少应该打印一些东西。log(arr[0])从数据库调用数据是一种异步的东西,javascript不会在任务完成之前等待您,除非您告诉它。在查询中使用async/awa

我正在使用StartWithIgnoreCase查询dexie数据库,并在数组中推送结果,但在打印或使用它时,它会抛出一个未定义的错误

我曾尝试使用JSON.stringify、toString、String将其转换为字符串并在控制台上打印,但仍然显示未定义

将整个阵列打印到控制台时,显示普通阵列()


当我使用console时,我至少应该打印一些东西。log(arr[0])

从数据库调用数据是一种异步的东西,javascript不会在任务完成之前等待您,除非您告诉它。在查询中使用async/await。大概是这样的:

async myControllerFunction()=>{
    arr = [];
    let firends = await db.table('friends').where('name').startsWithIgnoreCase('DoB/')
        .each(function (friend) {
            arr.push(String(friend.name));
        });
    console.log(arr[0]);
    console.log(arr);
}

当然,您无法在console.log()中看到arr[0],因为调用数据库查询始终是异步的。请尝试将查询包装为promise或async/Await。如果这样问很傻,我很抱歉,但(对于javascript来说是新的)我正在数组中推送正确显示的元素。当我使用console.log(arr)print在数组中显示正确的元素时,但是console.log(arr[0])您不能告诉我如何执行该操作,这就是为什么您看到数组“已满”,但
arr[0]
不存在
console.log(JSON.stringify(JSON.parse(arr))
,因此您记录了
arr
中的内容的副本,您将看到它是空的。
async myControllerFunction()=>{
    arr = [];
    let firends = await db.table('friends').where('name').startsWithIgnoreCase('DoB/')
        .each(function (friend) {
            arr.push(String(friend.name));
        });
    console.log(arr[0]);
    console.log(arr);
}