猿问

Dexie startsWithIgnoreCase() 函数数组未定义

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


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


将整个数组打印到控制台显示正常的 Array()


arr = [];

db.table('friends').where('name').startsWithIgnoreCase('DoB/')

                    .each(function (friend) {

                        arr.push(String(friend.name));

                    });

console.log(arr[0]); //undefined 

console.log(arr); //Array() with correct element inside

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


MM们
浏览 167回答 1
1回答

动漫人物

从数据库调用数据是异步的,除非你告诉它,否则 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);}
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答