I want to use the for loop outside of the query but it is printing empty.
sql = 'SELECT * FROM people';
let currMarker = [];
db.all(sql, [], (err,rows) => {
if (err) return console.error(err.message);
currMarker = rows;
for (let i = 0; i < currMarker.length; i++) {
console.log(currMarker[i].name);
console.log(currMarker[i].username);
}
});
I want to use it like :
sql = 'SELECT * FROM people';
let currMarker = [];
db.all(sql, [], (err,rows) => {
if (err) return console.error(err.message);
currMarker = rows;
});
for (let i = 0; i < currMarker.length; i++) {
console.log(currMarker[i].name);
console.log(currMarker[i].username);
}
Here's a solution.
sql = 'SELECT * FROM people';
let currMarker = [];
const queryFunc = () =>{
return new Promise((resolve, reject)=>{
db.all(sql, [], (err,rows) => {
if (err) return console.error(err.message);
currMarker = rows;
resolve();
});
});
};
queryFunc().then((val)=>{
for (let i = 0; i < currMarker.length; i++) {
console.log(currMarker[i].name);
console.log(currMarker[i].username);
}
});