AtiqGauri
AtiqGauri

Reputation: 1615

Dexie startsWithIgnoreCase() function array is undefined

I am querying dexie database with startsWithIgnoreCase and push result in an array but while printing or using it, it is throwing an error as undefined

I have tried using JSON.stringify, toString, String to convert it in a string and print on console but still it showing undefined

While print whole array to console showing normal 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

I should at least print something when i use console.log(arr[0])

Upvotes: 2

Views: 153

Answers (1)

Babak Abadkheir
Babak Abadkheir

Reputation: 2358

invoking data from database, is something asynchronous and javascript is not waiting for you till your task be done unless you told it. use async/await in your query. something like this:

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);
}

Upvotes: 2

Related Questions