Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

244
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

0

Try this

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

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda