Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular2 indexeddb如何从查询返回值_Angular_Return_Components_Indexeddb - Fatal编程技术网

Angular2 indexeddb如何从查询返回值

Angular2 indexeddb如何从查询返回值,angular,return,components,indexeddb,Angular,Return,Components,Indexeddb,我想从数据库中获取所有用户,为此我要: // 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); }); } 在我的组件中,我执行以下操作: // inp

我想从数据库中获取所有用户,为此我要:

// 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);
});
}
在我的组件中,我执行以下操作:

// input the result in the array.
const users: any = this.i.getAllUsers();
如果我尝试:

// 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);
});
}
我没有得到任何回报值

如果我这样做:

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

我得到了整个method对象…

也许我在想basic,但是如果你把它放在一个变量中,它能工作吗

// Get all users in the database.
getAllUsers() { 
this.db.getAll('users').then((users) => {
  var allUsers = users; // <-here
}, (error) => {
  console.log(error);
});
//获取数据库中的所有用户。
getAllUsers(){
this.db.getAll('users')。然后((users)=>{
var alluser=用户;//{
console.log(错误);
});
试试这个

getAllUsers() { 
 return this.db.getAll('users').then((users) => {
    return users;
   }, (error) => {
        console.log(error);
   });
}
问题解决了

  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);
});
}
构造函数(私有r:ComponentFactoryResolver,
私人rs:RestService,
私人d:数据服务,
private i:IndexedBService
) {
this.c=r.resolveComponentFactory(选择AccountProfileComponent);
this.db=新的AngularIndexedDB('usersDB',1);
this.db.createStore(1,(evt)=>{
const objectStore=evt.currentTarget.result.createObjectStore('users',{keyPath:'id',unique:true});
createIndex('membered','membered');
}).然后(()=>{
这个.getAllUsers();
},(错误)=>{
console.log(错误);
});
}
//获取数据库中的所有用户。
getAllUsers(){
this.db.getAll('users')。然后((users)=>{
设oldcounter=0;
常数kleurenArray=['#1ccc49'、'#e10d6d'、'#11afe0'、'#7dd317'、'#d95f16'、'#b090d'、'#7510cc'];
for(设i=0;i{
console.log(错误);
});
}

我必须将所有方法放在同一个组件中,然后才能使用该变量。无论如何,谢谢!

感谢您的回答,但我得到了以下对象:[Log]ZoneAwarePromise(main.bundle.js,第493行)\uuuuuuu zone\u symbol\uu状态:true\uuuuuu zone\u symbol\uu值:未定义的ZoneAwarePromise原型捕获(onRejected)构造函数:function()(OnImplemented,onRejected)对象原型(Uuuuuu defineGetter,Uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu你必须解决它。它正在返回一个承诺,所以在解决它之后,你将能够得到结果。