Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

241
Views
Angular2-indexeddb how to return the value from the query

i want to get all the users from the database, for this i do:

// Get all users in the database.
getAllUsers() { 
this.db.getAll('users').then((users) => {
  console.log(users); //this logs all the users in the console.
}, (error) => {
  console.log(error);
});
}

in my component i do this:

// input the result in the array.
const users: any = this.i.getAllUsers();

if i try:

// Get all users in the database.
getAllUsers() { 
this.db.getAll('users').then((users) => {
  return users; //i don't get a return value...
}, (error) => {
  console.log(error);
});
}

i dont get any return value...

if i do:

return this.db.getAll('users');

i get the whole method object...

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Maybe I'm thinking basic but does it work if you put it in a variable ?

// Get all users in the database.
getAllUsers() { 
this.db.getAll('users').then((users) => {
  var allUsers = users; // <-here
}, (error) => {
  console.log(error);
});
over 4 years ago · Santiago Trujillo Report

0

Try this

getAllUsers() { 
 return this.db.getAll('users').then((users) => {
    return users;
   }, (error) => {
        console.log(error);
   });
}
over 4 years ago · Santiago Trujillo Report

0

The problem is solved.

  constructor(private r: ComponentFactoryResolver,
          private rs: RestService,
          private d: DataService,
          private i: IndexedDBService
) {
this.c = r.resolveComponentFactory(ChooseAccountProfileComponent);
this.db = new AngularIndexedDB('usersDB', 1);

this.db.createStore(1, (evt) => {
  const objectStore = evt.currentTarget.result.createObjectStore('users', {keyPath: 'id', unique: true});

  objectStore.createIndex('remembered', 'remembered');

}).then(() => {
  this.getAllUsers();
}, (error) => {
  console.log(error);
});
}

// Get all users in the database.
getAllUsers() {
this.db.getAll('users').then((users) => {
  let oldcounter = 0;
  const kleurenArray = ['#1ccc49', '#e10d6d', '#11afe0', '#7dd317', '#d95f16', '#b0990d', '#7510cc'];

  for (let i = 0; i < users.length; i++) {
    // put the json in an object.
    const obj = users[i];
    // Make the component.
    const cmpRef = this.v.createComponent(this.c);
    // Fill the component with the correct values.
    cmpRef.instance.name = obj.username;
    cmpRef.instance.id = obj.id;
    do {
      var counter = Math.floor(Math.random() * 7);
    }
    while (oldcounter === counter);
    oldcounter = counter;
    cmpRef.instance.image = '../../../assets/images/Avatars/avatar-    bg-col' + counter + '.png';
    cmpRef.instance.color = kleurenArray[counter];
  }
}, (error) => {
  console.log(error);
});
}

I had to put all the methods in the same component, then I was able to use the variable. Thanks anyway!

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!